【发布时间】:2018-05-21 10:45:19
【问题描述】:
好的,所以我和我的朋友正在编写一个小型的基本酷刑游戏,当我尝试运行我的部分代码时,一些 if/elif 在它们看起来为 True 时不会像 False 一样运行
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 或大于 3,但您可以看到它不会。我和另一个有同样的问题。
对于第二个 while 函数,当我输入一个小于或大于 random_number(_1) 的数字时,它不会打印出更大或更低的数字,如果我输入 random_number(_1) 的值,它就不会运行。我看不出有什么问题,请帮忙
【问题讨论】:
-
乍一看,这可能是因为
input返回了str,而您在某个时候将int与此str进行了比较。 -
@Unatiel 你能指定在哪里吗?
标签: python python-2.7 loops conditional