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

标签 python random numbers pygame

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/

相关文章:

python - 如何在 python 中将变量从文本 "1m"更改为 "1000000"

一组数字中数字的重要性的php

ruby - 如何在 ruby​​ 中生成一个随机的 10 位数字?

c++ - boost c++ 使用 Mersenne Twister 算法在 0 和 1 之间生成相同的随机数?

python - 将 4 位整数转换为 boolean 列表

python - Django 电子邮件后端

random - Solidity - 生成不依赖于输入的不可预测的随机数

C++程序不读取公式

Python:如何将类变量设置为相同类型的另一个类

python - ssl SSLError outines :SSL_CTX_use_certificate_chain_file:PEM lib