功能是 class 忽略输入
Functions is the class ignores inputs
我试过更改“if”语句并使用 else 代替“elif”,但是在使用 Change 函数时它会忽略“n”或“N”输入。
class Settings:
def __init__(self):
self.text_speed = 1
self.companion_name = ("")
self.player_name = ("")
def Change_Text_Speed(self):
choice = int(input("Text Speed Options:\n1.5x [1] \n2x [2] \n2.5x [3] \nExit[4]"))
if choice == 1:
self.text_speed = (self.text_speed*1.5)
elif choice == 2:
self.text_speed = (self.text_speed*2)
elif choice ==3:
self.text_speed = (self.text_speed*2.5)
else:
print("No changes have been made...")
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice == 'y' or 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == 'n' or 'N':
print("No changes have been made...")
def Change_Player_Name(self):
choice = str(input("Do you wish to change your name?[Y/N]"))
if choice == 'y' or 'Y':
new_name = str(input("Enter in your new name: "))
self.player_name = new_name
elif choice == 'n' or 'N':
print("No changes have been made...")
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice == 'y' or choice == 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == 'n' or choice == 'N':
print("No changes have been made...")
我认为你应该像上面那样写你的 if 语句。现在你可以用 else 语句改变 elif。
您的 if 中不需要 or
。我看到两个解决方案:
使用是的答案列表:
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice in ['y', 'Y']:
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == ['n', 'N']:
print("No changes have been made...")
使用字符串upper
方法避免多选:
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice.upper() == 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice.upper() == 'N':
print("No changes have been made...")
我喜欢第一个解决方案,因为您可以使用更多选项,例如:
choice = str(input("Choice Yes or No.[Y/N]"))
yes_choices = ['YES', 'Y']
if choice.upper() in yes_choices:
print('You chose YES')
elif choice.upper() in ['NO', 'N']:
print('You chose NO')
我试过更改“if”语句并使用 else 代替“elif”,但是在使用 Change 函数时它会忽略“n”或“N”输入。
class Settings:
def __init__(self):
self.text_speed = 1
self.companion_name = ("")
self.player_name = ("")
def Change_Text_Speed(self):
choice = int(input("Text Speed Options:\n1.5x [1] \n2x [2] \n2.5x [3] \nExit[4]"))
if choice == 1:
self.text_speed = (self.text_speed*1.5)
elif choice == 2:
self.text_speed = (self.text_speed*2)
elif choice ==3:
self.text_speed = (self.text_speed*2.5)
else:
print("No changes have been made...")
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice == 'y' or 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == 'n' or 'N':
print("No changes have been made...")
def Change_Player_Name(self):
choice = str(input("Do you wish to change your name?[Y/N]"))
if choice == 'y' or 'Y':
new_name = str(input("Enter in your new name: "))
self.player_name = new_name
elif choice == 'n' or 'N':
print("No changes have been made...")
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice == 'y' or choice == 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == 'n' or choice == 'N':
print("No changes have been made...")
我认为你应该像上面那样写你的 if 语句。现在你可以用 else 语句改变 elif。
您的 if 中不需要 or
。我看到两个解决方案:
使用是的答案列表:
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice in ['y', 'Y']:
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice == ['n', 'N']:
print("No changes have been made...")
使用字符串upper
方法避免多选:
def Change_Companion_Name(self):
choice = str(input("Do you wish to change your companions name?[Y/N]"))
if choice.upper() == 'Y':
new_name = str(input("Enter in your companions new name: "))
self.companion_name = new_name
elif choice.upper() == 'N':
print("No changes have been made...")
我喜欢第一个解决方案,因为您可以使用更多选项,例如:
choice = str(input("Choice Yes or No.[Y/N]"))
yes_choices = ['YES', 'Y']
if choice.upper() in yes_choices:
print('You chose YES')
elif choice.upper() in ['NO', 'N']:
print('You chose NO')