【发布时间】:2020-10-20 03:40:30
【问题描述】:
random = random.randint(1000, 9999)
guess = int(input("Enter your guess: "))
while guess != random:
guess = int(input("That was incorrect! Enter your guess: "))
这是一个非常简单的猜谜游戏,但是我想添加一些内容,在每次尝试失败后,它会显示四位数字中有多少数字是正确的。
我没有尝试过,主要是因为我不确定如何做到这一点。
例如
random = 1234
Enter your guess: 1111
You guessed 1 number correct
Enter your guess: 1222
You guessed 2 numbers correct
...... and so on
【问题讨论】:
-
您可以使用
str(number)将这两个数字转换为字符串,然后逐个字符地遍历它们,并在两者中的字符相同时增加一个计数器变量。
标签: python random integer selection