【问题标题】:Conditionals won't work条件行不通
【发布时间】: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


【解决方案1】:

这里的问题是你的条件

guess > random_number

是一个 else if

guess_0 != random_number

和

guess_0 == random_number

意味着只有在前两者中没有一个评估为 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(....))

【讨论】:

  • 感谢您指出我已经对第一个 while 循环进行了更改,但我对第二个 while 循环也有问题。我的意思是不工作,例如当 random_number = 43 并且当我输入 43 时,它仍然会产生“再试一次”结果。
  • 另一个例子是当我输入 2 并且 random_number = 43 但它会打印出“lower”
  • 您忘记将 raw_input 转换为 int。它应该是guess_1=int(raw_input(...))。
【解决方案2】:

您可以避免使用while 循环,除非您有义务,使用for 循环可能会更好。

如果退出循环条件出现错误,while 循环可能以无限循环结束,这可能导致计算机冻结或资源负载过重...

【讨论】:

  • 谢谢,但出于某种原因,我更容易理解 while 循环而不是 for 循环,但我现在会了解更多
【解决方案3】:

也许您可以尝试将条件简化为这个?

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

此原则也应适用于您询问的第二个循环。

【讨论】:

  • 谢谢你,我已经做出了改变,但由于某种原因,我的第二个 while 循环会打印出奇怪的东西,比如当我输入 2 和 random_number = 43 但它会打印出“lower”或者当我输入 43 但结果不正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2018-05-06
  • 2010-12-03
相关资源
最近更新 更多