2016-06-07 114 views
2

我在下面有这个功能,我在某处做了某些错误。循环功能

def quantityFunction(product): 
    valid = False 
    while True: 
     if product is not None: 
      quantity = input("Please enter the amount of this item you would like to purchase: ") 
      for i in quantity: 
       try: 
        int(i) 
        return int(quantity) 
        valid = True 
       except ValueError: 
        print("We didn't recognise that number. Please try again.") 
        #If I get here, I want to loop back to the start of this function 
        return True 

     return False 

要运行通过,该功能是从程序的主要部分被称为像这样:quantity = quantityFunction(product)

返回的代码的底部假是,如果产品是无,这是做需要在另一个函数中的一些代码,但不得不在这个功能。

如果用户输入数量是一个数字,所有工作正常。如果是其他内容,则会打印“值错误”,您可以输入另一个输入。如果你再写一封信等,它会再次重复,如果你输入一个数字,它会接受它。

但是,它不会返回您在字母后输入的数字。它只返回0.

我怀疑这是关于如何重复代码,即代码应该循环回到函数的开始,如果它击中值错误。

任何想法?

+0

你在return块中有return语句。只需打印错误消息,它应该可以正常工作。 – SilentMonk

+2

为什么你定义'valid'?它从未使用过。为什么是for循环?为什么不尝试将数量直接转换为int? – Ben

+0

现在我已经把除了块以外的函数返回,函数不会循环,我怎么能这样做呢? @SilentMonk –

回答

3

你说:

代码应该环回功能的开始,如果它击中值误差。

那么你不应该使用return声明,否则该功能将终止,返回TrueFalse

+0

好吧,我已经从外面的区块中退出了。现在函数不会循环,我怎么能这样做呢? @heltonbiker –

+0

我纠正了答案,你有两个回报,一个是例外,另一个是正常流量。同时删除 – heltonbiker

+0

我认为你对'return'和循环有一个错误的想法。你有什么意图返回“真”和“假”? – heltonbiker

1

你应该试试这个..

def quantityFunction(product): 
    valid = False 
    while True: 
     if product is not None: 
      quantity = raw_input("Please enter the amount of this item you would like to purchase: ") 

      if quantity.isdigit(): 
      return int(quantity) 
      valid = True 
      else: 
      print("We didn't recognise that number. Please try again.") 
      continue 

     return False 

quantity = quantityFunction("myproduct") 
3

几个问题:

1)return语句控制返回给调用函数。

2)您正在循环输入,这是错误的。

3)valid=True根本不执行。

def quantityFunction(product): 
    valid = False 
    while True: 
     if product is not None: 
      quantity = raw_input("Please enter the amount of this item you would like to purchase: ") 
      try: 
        return int(quantity) 
        #valid = True (since it is never run) 
      except ValueError: 
        print("We didn't recognise that number. Please try again.") 
        #If I get here, I want to loop back to the start of this function 
        #return True 
     return False 

quantityFunction("val") 

注意:使用raw_input()在Python 2.7和input()的情况下3.X

1

首先情况下,我们解决的代码。简单地说,你需要一个循环直到用户输入合法数量的函数。

产品对功能没有太大作用;请在调用程序中检查它,而不是在这里。让函数有一个目的:获取有效数量。

让我们从标准配方开始“从循环到完美输入”。很简单,它看起来像:

Get first input 
Until input is valid 
... print warning message and get a new value. 

在代码中,它看起来像这样。

def get_quantity(): 
    quantity_str = input("Please enter the amount of this item you would like to purchase: ") 

    while not quantity_str.isdigit(): 
     print("We didn't recognise that number. Please try again.") 
     quantity_str = input("Please enter the amount of this item you would like to purchase: ") 

    return quantity 

至于编码习惯...

逐步开发:写几行代码添加一个功能,你有什么。调试。在添加更多内容之前让它工作。

了解您的语言功能。在您发布的代码中,您误用代替,代码,返回代码和函数调用。

看看如何解决简单的问题。 尝试/除了是一个更难处理的概念比简单isdigit

1

试试这个(也包含一些格式,但功能应该是相同的):

def determine_quantity(product): # descriptive function name 
    if not product: # avoiding nesting 
     return False 
    while True: 
     quantity = input("Please enter the amount of this item you would like to purchase: ") 
     try: 
      return int(quantity) # try to convert quantity straight away 
     except ValueError: 
      print("We didn't recognise that number. Please try again.") 
      # nothing here means we simply continue in the while loop 

理想情况下,你会拿产品出来。函数应该尽可能少,而且这个检查在其他地方更好。

def determine_quantity(): 
    while True: 
     quantity = input("Please enter the amount of this item you would like to purchase: ") 
     try: 
      return int(quantity) 
     except ValueError: 
      print("We didn't recognise that number. Please try again.")