所以我仍在为我的游戏编码,而且我正处于最后的一段时间。
我需要知道如何检查变量中是否有除整数之外的任何字符。
以下是我的代码现在的样子。
import random
random = random.randint(1001,10000)
random = str(random)
print(random)
count = 0
while True:
correct = 0
count = count + 1
guess = input("Guess a random 4 digit number")
#guess = str(guess)
if guess != str(guess):
if len(guess) == 4:
for i in range(4):
if guess[i] == random[i]:
correct = correct + 1
if guess == random:
print("Well done you guessed the number correctly in", count, "tries")
break
else:
print("Wrong you got", correct, "correct")
else:
print("Must Be A 4 Digit Number!")
else:
print("It Must Be A Number!")
如果您要粘贴此代码,则因缩进而错误。
谢谢,
答案 0 :(得分:1)
尝试字符串的isdigit()方法:
'123213'.isdigit()
True
'123213dsd'.isdigit()
False