好吧,所以我和我的朋友正在编写一个小型的基本酷刑游戏,当我尝试运行我的部分代码时,一些if / elif它们看起来无法运行就好像它们是假的
lives = 5
lives_1 = 13
random_number = random.randint(0,100)
random_number_1 = random.randit(0,1000)
while lives > 0:
print random_number # delete in final product
print "'You have %d chances to save your partner'" % (lives)
guess_0 = int(raw_input("Guess a number from 1 to 100 "))
print "You have guessed %d" % (guess_0)
proceed_2 = raw_input("Press 'Enter' to proceed")
if guess_0 != random_number: # This works
print "It is incorrect, your partner loses a finger"
lives -= 1
continue
elif guess_0 == random_number: # This works
print "It is correct you and your partner get to walk free"
print "GAME OVER"
break
elif guess > random_number: # Doesn't work
print "'LOWER!'"
elif guess < random_number: # Doesn't work
print "'HIGHER!'"
else:
print "GAME OVER"
while lives_1 > 0:
print random_number_1 # delete in final product
print "'You ha-- %d ch---e-'" % (lives_1)
guess_1 = raw_input("What should you do? ")
proceed_1 = raw_input("Press Enter to proceed")
print "You decided to shout {}".format(guess_1)
if guess_1 == random_number_1:
print "The cult has decided to let you go" # doesn't work
break
elif guess_1 != random_number_1: # Works but also works when false
lives_1 -= 1
if lives_1 < 5:
print "Tears begin to blind you as you're nearing death"
continue
elif guess_1 < random_number_1: # doesn't work
print "You squeel in agony as one of your nails were ripped off"
print "'Higher'"
elif guess_1 > random_number_1: #doesn't work
print "You squeel in agony as one of your nails were ripped off"
print "'LOWER...'"
else:
print "GAME OVER"
我希望它打印出大于或大于3但是你可以看到它不会。我和另一个有同样的问题。
对于第二个while函数,当我输入一个低于或大于random_number(_1)的数字时,它不会打印出更大或更低的值,如果我输入random_number(_1)的值,它就赢了“t”跑。我看不出有什么问题请帮忙
答案 0 :(得分:1)
这里的问题是你的情况
guess > random_number
是
的其他内容guess_0 != random_number
和
guess_0 == random_number
意味着只有前两者中的NONE评估为True才会对其进行评估。正如你所看到的,情况并非如此。你可以这样写:
if guess_0 == random_number:
print "It is correct you and your partner get to walk free"
print "GAME OVER"
break
if guess_0 != random_number:
print "It is incorrect, your partner loses a finger"
lives -= 1
if guess > random_number:
print "'LOWER!'"
elif guess < random_number:
print "'HIGHER!'"
在你的第二个while循环中你应该编辑行
guess_1=raw_input(....)
到
guess_1=int(raw_input(....))
答案 1 :(得分:0)
也许你可以尝试简化你的条件吗?
if guess_0 == random_number:
print "It is correct you and your partner get to walk free"
print "GAME OVER"
break
else:
print "It is incorrect, your partner loses a finger"
lives -= 1
if guess_0 > random_number:
print "'LOWER!'"
else:
print "'HIGHER!'"
这个原则也应该适用于你问过的第二个循环。
答案 2 :(得分:0)
在您有义务之前,您可以避免使用while
循环,使用for
循环可能会更好。
如果退出循环条件出现错误,while
循环可能以无限循环结束,这可能导致计算机冻结或重载资源负载......