import random
thenum = random.randint(1000, 9999)
print (thenum)
tally = 1
guessone = int(input("What is your first guess?: "))
strguessone = str(guessone)
strthenum = str(thenum)
if strguessone [0] == strthenum [0]:
    tally + 1
if  strguessone [1] ==  strthenum [1]:
    tally + 1
if strguessone [2] == strthenum [2]:
    tally + 1
if  strguessone [3] ==  strthenum [3]:
    tally + 1

print (tally)
if tally == 5:
    print ("You Win")


当我运行这段代码时,我仅从打印计数中得到一个响应,为什么在我知道应该使用循环的情况下将计数添加到if语句中,但是我发现这就是我想做的

最佳答案

您应该使用+ =运算符而不是+运算符。

import random
thenum = random.randint(1000, 9999)
print (thenum)
tally = 1
guessone = int(input("What is your first guess?: "))
strguessone = str(guessone)
strthenum = str(thenum)
if strguessone [0] == strthenum [0]:
    tally += 1
if  strguessone [1] ==  strthenum [1]:
    tally += 1
if strguessone [2] == strthenum [2]:
    tally += 1
if  strguessone [3] ==  strthenum [3]:
    tally += 1

print (tally)
if tally == 5:
    print ("You Win")

关于python - 不会添加提示策划游戏python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32663828/

10-10 22:46