从零到一,然后要求从零到一的另一个值

时间:2018-08-07 16:35:26

标签: python python-3.x while-loop user-input

我对python还是很陌生,但我仍然对如何执行此操作感到困惑:

我构建了一个使用while循环的程序,通过该循环我可以通过以下方式获得从0到1的值:

while True:
  x= input("Enter a number from 0 to 1: ")
  try:
    if float(x)>=0 and float(x)<=1:
      print("Congrats!")
      break
    elif float(x) < 0:
      print("This number is negative")
    elif float(x) > 1:
      print("This number is too high!")
    else:
      print("Try again")
  except ValueError:
    print("This is not a number")

还有另一个程序,我使用while again == 'y' or 'Y':

进行while循环

但是我似乎无法将两者结合起来。 我想要一个程序,该程序首先获得一个从0到1的值,然后询问用户是否要通过y或n输入另一个数字。

我尝试这样做无济于事:

def ask_input():
  while again == 'y' or 'Y':
    while True:
      x= input("Enter a number from 0 to 1: ")
      try:
        if float(x)>=0 and float(x)<=1:
          print("Congrats!")
          again = input('do you want again? y or n: ')
          break
        elif float(x) < 0:
          print("This number is negative")
        elif float(x) > 1:
          print("This number is too high!")
        else:
          print("Try again")
      except ValueError:
        print("This is not a number")

ask_input()

还要如何将所有这些都放入列表中?填充的数字范围是0到1?

0 个答案:

没有答案
相关问题