【问题标题】:Why doesn't this number guessing program continue past this line?为什么这个猜数字程序不继续越过这条线?
【发布时间】:2016-08-01 23:10:52
【问题描述】:

以下代码用于猜数字游戏。它没有错误,但没有给出输出。如何进一步改进代码?

它一直工作到print("Your Number has been Generated,Enter your guess Below") 这一行,但没有给出任何输出。

     #Guess the number

     from random import randint

     print("Welcome to Guess the Number Game:")
     print("Press 'g' to Generate Number between 0 and 100:")

     cmd=input("->")

    if cmd=='g':
       num=randint(0,100)
    else:
       print("Unknown Input")


    def chk(x):
        if int(x)==True:
           chk=True
        else:
           chk=False

    def diff(x,y):
        diff=(x-y)
        return diff

    print("Your Number has been Generated,Enter your guess Below")
    guess=input("->")

    chk(guess)

    while chk==True:
    diff(num,guess)
    if diff==0:
    print("Congrats! correct Guess")

    elif diff>10:
    print("Oh! too high")

    elif diff<5 and diff>0:
    print("very close!Better Luck Next time")

    else:
    diff<0
    print("Sorry!too Low")

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    已经给出了很好的答案,但是您可以使用非常“扁平”的脚本来做到这一点,而无需定义函数。这仅作为示例:

    from random import randint
    
    # welcome
    print("Welcome to Guess the Number Game:")
    
    # loop until 'g' is given
    cmd = ''
    while not cmd == 'g':
        print("Press 'g' and '<enter>' to Generate Number between 0 and 100:")
        cmd = input("->")       
    
    # generates a random number
    num = randint(0,100)
    print("Your Number has been Generated,Enter your guess Below")
    
    # loops until correct answer is guessed
    while True:
        try:
            guess=int(input("guess->"))
        except ValueError:
            print('Numbers only please')
            continue
        if guess > num: 
            print('too high')
        if guess < num: 
            print('too low')        
        if guess == num:
            print("Congrats! correct Guess")
            break
    

    【讨论】:

      【解决方案2】:

      您有几个问题,其中一些可能是转录问题:

      def chk(x):
          if int(x)==True:
             chk=True
          else:
             chk=False
      

      我想这是为了check 输入。根据 python 的版本,这将始终将 chk 设置为 false。这是因为int 返回一个整数,它与布尔值True 的类型不同。目前尚不清楚您要在这里实现什么。看起来您想检查输入是否为数字。相反,为什么不使用isdigit?因为,目前,您的代码返回 false 您永远不会进入 while 循环

      您的 while 循环也没有正确缩进,根据经验,ifwhile 语句之类的语句之后需要换行和缩进。

      所以这可能是更好的代码:

      # keep guessing while it's a digit
      while guess.isdigit():
          # assign the difference to a variable
          # don't forget to indent after the while statement
          diff = diff(num, int(guess))
          if diff==0:
              # indent again!
              print("Congrats! correct Guess")
      
          elif diff>10:
              print("Oh! too high")
      
          elif diff<5 and diff>0:
              print("very close!Better Luck Next time")
      
          elif diff<0:
              print("Sorry!too Low")
      

      这样你就根本不需要chk

      【讨论】:

      • 你是对的,我使用 def chk(x) 来检查输入是否为数字,我会牢记缩进规则。感谢您的帮助
      • 我在 chk(x) 中的想法是,如果输入不是数字,即如果是字符串,则 int(string) 将不可能,因此条件 int(x)==真会假,现在我明白了。感谢您的洞察力
      • @RohanDwivedi 实际发生的是抛出异常。
      【解决方案3】:

      有多个问题,我已经使用 cmets 展示了这一点

      from random import randint
      
      print("Welcome to Guess the Number Game:")
      print("Press 'g' to Generate Number between 0 and 100:")
      
      cmd=input("->")
      
      if cmd=='g':
             num=randint(0,100)
      else:
             print("Unknown Input")
      
      
      def chk(x):
          if x.isnumeric(): # needs to be '.isNumeric()' as only the only integer that will return true is '1'
                 chk=True
          else:
                  chk=False
          return chk #Returns Boolean
      
      def diff(x,y):
              return (y-x)# this needs to y-x to be correct
      
      print("Your Number has been Generated,Enter your guess Below")
      guess=input("->")
      
      while chk(guess): #or chk(guess) == True
          difference = diff(num,int(guess))#guess needs to be an INTEGER
          if difference==0:
              print("Congrats! correct Guess")
              exit()
      
      if difference>10:
          print("Oh! too high")
      
      elif difference<5 and difference>0:
          print("very close!Better Luck Next time")
      
      elif difference<0:
          print("Sorry!too Low")
      print("Enter your guess Below")#allows you to put in another guess
      guess=input("->")#sets the guess variable
      chk(guess)
      

      【讨论】:

        猜你喜欢
        • 2021-03-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-11
        • 1970-01-01
        • 1970-01-01
        • 2013-02-08
        • 1970-01-01
        • 2017-12-08
        相关资源
        最近更新 更多