gpt4 book ai didi

python - 如何检查变量中是否存在除整数之外的任何内容?

转载 作者:太空宇宙 更新时间:2023-11-03 15:05:05 25 4
gpt4 key购买 nike

所以我仍在编写我的游戏,并且我正处于最后的验证阶段。

我需要知道如何检查变量中是否有除整数之外的任何字符。

这是我的代码现在的样子。

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!")

如果您粘贴此代码,由于缩进,将会出现错误。

谢谢

最佳答案

尝试字符串的 isdigit() 方法:

'123213'.isdigit()
True

'123213dsd'.isdigit()
False

关于python - 如何检查变量中是否存在除整数之外的任何内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44735949/

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