gpt4 book ai didi

python - 不会添加计数mastermind游戏python

转载 作者:行者123 更新时间:2023-12-01 04:29:02 25 4
gpt4 key购买 nike

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 - 不会添加计数mastermind游戏python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32663828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com