0

我尝试过更改“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...")
4

2 回答 2

1

你不需要or在你的 if 中。我看到两个解决方案:

使用 YES 答案列表:

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')
于 2020-11-30T12:09:13.413 回答
0
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。

于 2020-11-30T11:58:33.780 回答