在Y / N上我希望小写y能够准确地响应大写字母Y的方式

时间:2017-03-29 17:26:35

标签: python

当它询问用户是否愿意进行硬币投掷模拟(Y / N)时#34;我希望用户能够输入小写y和大写Y,并让他们都做同样的事情。我把小写字母y放的是它给了我再见的信息,好像用户输入了N

处理这个问题的领域是7-9和32-34号线,非常感谢任何帮助,非常感谢

import random
def main():
    name = input("Hello user, please enter your name: ")
    print("Hello", name , "This program runs a coin toss simulation")
    yn = input("Would you like to run the coin toss simulation?(Y/N): ")
    if yn != 'Y':
        print("Ok have a nice day!")
        return

    while True:
        heads = tails = 0
        while True:
            count = int(input("Enter the amount of times you would like the coin to flip: "))
            if count <= 0: 
                print("Silly rabbit, that won't work")
            else:
                break

        while tails + heads < count:
            coin = random.randint(1, 2)
            if coin ==1:
                heads = heads + 1
            else:
                tails = tails + 1

        print("you flipped", count , "time(s)")
        print("you flipped heads", heads , "time(s)")
        print("you flipped tails", tails , "time(s)")

        yn = input("Would you like to run another coin toss simulation?(Y/N): ")
        if yn != 'Y':
            print("Ok have a nice day!")
            return

main()的

2 个答案:

答案 0 :(得分:2)

使用.lower()方法:

if yn.lower() != 'y'

您也可以使用.upper()

if yn.upper() != 'Y'

然而,程序员很懒,并且希望尽可能避免使用Shift键,因此.lower()的持续流行。

答案 1 :(得分:0)

给他们一些选择

if yn.lower() not in ('y', 'yes'):
    ...