Python 为什么要忽略多行?
我试图做一个破译游戏,但由于某些原因,它不工作,请帮助Python 为什么要忽略多行?,python,Python,我试图做一个破译游戏,但由于某些原因,它不工作,请帮助 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
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
当您执行str(a)时,它不会将a转换为字符串。为此,请执行a=str(a)
由于a当前是一个整数,当您稍后将a
与ag
等进行比较时,检查失败
您需要对当前代码中的b
、c
、d
执行相同的操作
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!"
现在,我认为您找到了解决此问题的正确方法。您很可能需要将
字符串
值从STDIN转换为整数
值-。此外,考虑制作<代码>完成< <代码> >代码BooLoe< /Cord>。@ Trand and MistelyError:如果您的查询已被解决,请不要忘记:“ObjISSpIDER,因为OP使用STR(A),所以我认为这是更好的方式来指出…当然,另一种方法也适用。