Python 2.7 Python中While循环的问题

Python 2.7 Python中While循环的问题,python-2.7,Python 2.7,我想创建一个名为“猜数字”的游戏 我希望程序生成一个随机数,并将其与用户输入的值进行比较,然后将正确/错误的次数写入文本文件 程序的第一部分工作正常,问题出现在最后,我请求用户输入以决定是否退出/继续程序 它不断要求用户输入是否退出/继续 例:- 欢迎来到猜数字游戏 输入一个介于1到10:4之间的数字 你猜错数字了 按Q退出,或按E继续:E 按Q退出,或按E继续:Q 按Q退出,或按E继续:E 不要退出或继续程序,下面是我的代码,请帮助我。只是Python初学者,自学成才 import rand

我想创建一个名为“猜数字”的游戏

我希望程序生成一个随机数,并将其与用户输入的值进行比较,然后将正确/错误的次数写入文本文件

程序的第一部分工作正常,问题出现在最后,我请求用户输入以决定是否退出/继续程序

它不断要求用户输入是否退出/继续

例:-

欢迎来到猜数字游戏

输入一个介于1到10:4之间的数字

你猜错数字了

按Q退出,或按E继续:E

按Q退出,或按E继续:Q

按Q退出,或按E继续:E

不要退出或继续程序,下面是我的代码,请帮助我。只是Python初学者,自学成才

 import random


print "Welcome to the Guess the Number game!"
print

c = True
lost = 0
win = 0
j = ""
d = True

while c == True:
    v = random.randint(1,10)
    n = input("Enter a number between 1 to 10 : ")
    print
    if n > 10:
        print "The number you entered is above 10, please enter a number below 10."
        print
    elif n < 0:
        print "The number you entered is below 0, please enter a number above 0."
        print
    elif 0<n<10:
        print "Progressing..."
        print
    else:
        j = " "
    if n == v:
        print "Congratulations, you guess the number correctly!"
        print
        win = win + 1
        file = open("text.txt","w") 
        file.write("Number of times won : %s \n" %(win))
        file.close()
    else:
        print "You guess the number incorrectly!"
        print        
        lost = lost + 1
        file = open("text.txt","w") 
        file.write("Number of times lost : %s \n" %(lost))
        file.close()      
    while d == True:
        response =  raw_input("Press Q to quit or E to continue : ")
        response = str(response)          
        print        
        if response == 'Q':
            c = False
            d == False
        elif response == 'E':
            c = True
            d == False
        else:
            print("Please read the instructions properly and press the proper button.")
            print        
            d == True
            
随机导入
打印“欢迎参加猜数字游戏!”
打印
c=真
丢失=0
赢=0
j=“”
d=真
当c==True时:
v=随机随机随机数(1,10)
n=输入(“输入一个介于1到10之间的数字:”)
打印
如果n>10:
打印“您输入的数字高于10,请输入低于10的数字。”
打印
elif n<0:
打印“您输入的数字低于0,请输入高于0的数字。”
打印

elif 0您的问题在这个while循环中

while d == True:
    response =  raw_input("Press Q to quit or E to continue : ")
    response = str(response)          
    print        
    if response == 'Q':
        c = False
        d == False
    elif response == 'E':
        c = True
        d == False
    else:
        print("Please read the instructions properly and press the proper button.")
        print        
        d == True
=(赋值运算符)和==(比较运算符)之间有很大区别。第一个(=)将重写变量并将新值存储在内存中,然而,它只会比较左边的和右边的,当你把d==设为False或True时,这是非常重要的,你永远不会重新分配变量,它将永远留在这个循环中。要更正它,您只需将其更改为此

while d == True:
    response =  raw_input("Press Q to quit or E to continue : ")
    response = str(response)          
    print        
    if response == 'Q':
        c = False
        d = False
    elif response == 'E':
        c = True
        d = False
    else:
        print("Please read the instructions properly and press the proper button.")
        print        
        d = True

您的问题在这个while循环中

while d == True:
    response =  raw_input("Press Q to quit or E to continue : ")
    response = str(response)          
    print        
    if response == 'Q':
        c = False
        d == False
    elif response == 'E':
        c = True
        d == False
    else:
        print("Please read the instructions properly and press the proper button.")
        print        
        d == True
=(赋值运算符)和==(比较运算符)之间有很大区别。第一个(=)将重写变量并将新值存储在内存中,然而,它只会比较左边的和右边的,当你把d==设为False或True时,这是非常重要的,你永远不会重新分配变量,它将永远留在这个循环中。要更正它,您只需将其更改为此

while d == True:
    response =  raw_input("Press Q to quit or E to continue : ")
    response = str(response)          
    print        
    if response == 'Q':
        c = False
        d = False
    elif response == 'E':
        c = True
        d = False
    else:
        print("Please read the instructions properly and press the proper button.")
        print        
        d = True

哦,天哪,谢谢!糟糕,我只是复制并粘贴了“d==false”部分!哦,天哪,谢谢!糟糕,我只是复制并粘贴了“d==false”部分!