我试图制作一个破解密码游戏,但由于某些原因它无法正常工作请帮助
done = 0
guess = 0
import random
a=random.randrange(0,9)
str(a)
b=random.randrange(0,9)
str(b)
c=random.randrange(0,9)
str(c)
d=random.randrange(0,9)
str(d)
print a,b,c,d #Its on so I can guess the correct number
while done == 0:
ag=raw_input("Input a single digit between 9 and 0 : ")
bg=raw_input("Input a single digit between 9 and 0 : ")
cg=raw_input("Input a single digit between 9 and 0 : ")
dg=raw_input("Input a single digit between 9 and 0 : ")
print ag,bg,cg,dg
if ag == a and bg == b and cg == c and dg == d:
print "Well Done! You Got it correct in " ,guess, " times!"
done = 1
else:
print "Incorrect"
if ag == a :
print "You succsessfully guessed the first number!"
if bg == b :
print "You succsessfully guessed the second number!"
if cg == c :
print "You succsessfully guessed the third number!"
if dg == d :
print "You succsessfully guessed the forth number!"
我认为这应该有效,因为当我得到一些正确的数字时,它会打印出你得到的" n"数字是正确的,但总是出现这个:
>> 5 8 3 3
>> Input a single digit between 9 and 0 : 5
>> Input a single digit between 9 and 0 : 8
>> Input a single digit between 9 and 0 : 3
>> Input a single digit between 9 and 0 : 3
>> 5 8 3 3
>> Incorrect
答案 0 :(得分:2)
执行str(a)
时,它不会将a转换为字符串。为此,请a = str(a)
。
由于a当前是整数,后来将a
与ag
进行比较等等,检查失败。
您需要对当前代码中的b
,c
,d
执行相同操作。
答案 1 :(得分:-1)
done = 0
guess = 0
import random
a=random.randrange(0,9)
b=random.randrange(0,9)
c=random.randrange(0,9)
d=random.randrange(0,9)
print a,b,c,d #Its on so I can guess the correct number
while done == 0:
ag=int(raw_input("Input a single digit between 9 and 0 : "))
bg=int(raw_input("Input a single digit between 9 and 0 : "))
cg=int(raw_input("Input a single digit between 9 and 0 : "))
dg=int(raw_input("Input a single digit between 9 and 0 : "))
print ag,bg,cg,dg
if ag == a and bg == b and cg == c and dg == d:
print "Well Done! You Got it correct in " ,guess, " times!"
done = 1
else:
print "Incorrect"
if ag == a :
print "You succsessfully guessed the first number!"
if bg == b :
print "You succsessfully guessed the second number!"
if cg == c :
print "You succsessfully guessed the third number!"
if dg == d :
print "You succsessfully guessed the forth number!"
现在我认为你有正确的想法如何解决这个问题。