【问题标题】:Using too many "while True:" loops?使用太多“while True:”循环?
【发布时间】:2020-05-25 21:06:05
【问题描述】:

我的代码将在下面。我正在练习 Python,想做一个简单的餐厅小费计算器。它工作正常,但我觉得我过于依赖 while 循环。对于每个输入提示,我想让脚本检查输入是否为 int/float,如果不是,它将继续循环,直到给出正确的输入。

是否有人对这是否是进行此类“检查”的好方法有建议?

还有:

在我将“#DETERMINING TIP PERCENTAGE BASED ON QUALITY”和后面的部分放在他们自己的 while 循环中(它们最初是在代码末尾而不是循环中)之前,我无法让脚本移动在“您的服务质量如何?”之后的那些部分。部分。该脚本仅循环播放该部分。知道为什么吗?它的格式似乎与之前的其他 2 个 while 循环完全相同。

谢谢大家。这是我的代码:

while True :
    try :
        total = float(input('Enter the total cost of the meal: '))
        if total < 1 :
            print('Please enter a value greater than 0. ')
            continue
    except :
        print('Please enter a numerical value. ')
        continue

    while True :
        try :
            guests = int(input('How many guests? '))
            if guests < 1 :
                print('Please enter a value greater than 0. ')
                continue
        except :
            print('Please enter a numerical value. ')
            continue

        while True :
            try :
                quality = int(input('How was the quality of your service? 1 - 3: '))
                if quality > 3 or quality < 1 :
                    print('Please enter a value between 1 and 3. ')
                    continue
            except :
                print('Please enter a numerical value. ')
                continue

            #DETERMINING TIP PERCENTAGE BASED ON QUALITY
            while True :
                if quality == 1 :
                    tip_percent = 0.15
                elif quality == 2 :
                    tip_percent = 0.20
                elif quality == 3 :
                    tip_percent = 0.25

                #CALCULATING PER PERSON TIP AMOUNT
                gross_tip = total * tip_percent
                perperson_tip = (total / guests) * tip_percent

                #PRINTING TIP PER PERSON AND GROSS TIP
                print('Per-person tip: ' + str(perperson_tip) + '\nGross tip: ' + str(gross_tip))
                exit()

【问题讨论】:

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


    【解决方案1】:

    您的主要问题是您的所有 while 循环都相互嵌套。没有充分的理由。

    while True:
      try:
        total = float(input('Enter the total cost of the meal: '))
      except:
        print('Please enter a numerical value.')
        continue
      if total < 1:
        print('Please enter a value greater than 0.')
        continue
      break
    
    while True:
      try:
        guests = int(input('How many guests? '))
      except:
        print('Please enter a numerical value. ')
        continue
      if guests < 1:
        print('Please enter a value greater than 0. ')
        continue
      break
    
    # and so on
    

    【讨论】:

    • 我还会将循环 + 输入检查重构为一个函数以避免如此多的重复。
    • 0x5453,它需要将转换器和条件作为参数传递。条件将是 lambdas。对于 Python 学徒来说,它们是困难的概念。
    【解决方案2】:

    您可以使用您的接受条件来打破while 内的循环。 那么你可以完全避免明确的breakcontinue 和奇怪的无尽while True

    total = 0
    while total < 1:
        try :
            total = float(input('Enter the total cost of the meal: '))
            if total < 1 :
              print('Please enter a value greater than 0. ')
        except :
            print('Please enter a numerical value. ')
    
    guests = 0
    while guests < 1:
        try :
            guests = int(input('How many guests? '))
            if guests < 1 :
                print('Please enter a value greater than 0. ')
        except :
            print('Please enter a numerical value. ')
    
    quality = 0
    while quality > 3 or quality < 1  :
        try :
            quality = int(input('How was the quality of your service? 1 - 3: '))
            if quality > 3 or quality < 1 :
                print('Please enter a value between 1 and 3. ')
        except :
            print('Please enter a numerical value. ')
    
    
    
    if quality == 1 :
        tip_percent = 0.15
    elif quality == 2 :
        tip_percent = 0.20
    elif quality == 3 :
        tip_percent = 0.25
    
    #CALCULATING PER PERSON TIP AMOUNT
    gross_tip = total * tip_percent
    perperson_tip = (total / guests) * tip_percent
    
    #PRINTING TIP PER PERSON AND GROSS TIP
    print('Per-person tip: ' + str(perperson_tip) + '\nGross tip: ' + str(gross_tip))
    

    【讨论】:

      猜你喜欢
      • 2017-07-14
      • 2020-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2016-12-03
      • 2014-07-30
      相关资源
      最近更新 更多