简体   繁体   English

循环输入提示计算器(Python)

[英]Loop in Tip Calculator (Python)

So I'm making a tip calculator at the moment. 所以我现在正在制作一个小费计算器。 The thing that I am stuck on is where they can input the total amount of the cost. 我坚持的事情是他们可以输入成本的总额。 If they input an integer I want it to break out of the loop but if they input something else than an integer, I want it to stay in the loop and tell them to enter an integer. 如果他们输入一个整数,我希望它跳出循​​环,但是如果他们输入一个非整数的值,我希望它留在循环中并告诉他们输入一个整数。 Here is the code that I made for this portion. 这是我为这部分编写的代码。 (Not all the code) (并非所有代码)

Integer = range(1,10000)




while True:
    while True:
        Cost = raw_input("What was the cost? ")
        Cost = int(Cost)
        if Cost in Integer:
            break
        else:
            pass

The spacing may not look correct but it is in the actual script. 间距看起来可能不正确,但是在实际脚本中。 I still don't know how to paste the code on here without having to add 4 spaces to every line. 我仍然不知道如何在此处粘贴代码,而不必在每行中添加4个空格。 Anyway, please let me know what you would do to complete the task I need. 无论如何,请让我知道您将如何完成我需要的任务。

Casting a String object to an int can raise a ValueError exception, however since raw_input() returns a str object you can easily check if it's all digits with isdigit() . 将String对象转换为int会引发ValueError异常,但是由于raw_input()返回str对象,因此您可以轻松地检查isdigit()是否全部为数字。 The full documentation of the isdigit() is found here isdigit()的完整文档可在此处找到

if cost.isdigit():
  cost = int(cost)
  break
else:
  cost = raw_input("What is the cost? ")

That was problem number 1. Problem number 2 you're facing is if Cost in Integer . 那就是问题1。问题2是if Cost in Integer

This is not how that works, you're probably after if isinstance(cost, int): because you want to check if it's an integer after all (since you're converting it) 这不是这样的,可能是在if isinstance(cost, int):因为您想检查它是否毕竟是整数(因为要转换它)

and lastly: 最后:

You should not use while True , while this is working for you you wont be able to break it since you haven't assigned True to a variable. 您不应该while True时使用while True ,因为它没有用,因为您没有将True分配给变量,因此您将无法破坏它。

outer = True
inner = True

while outer:
  while inner:
    #your code here
    inner = False #now it will break automatically from the inner loop.

Cost = int(Cost) will raise a ValueError if Cost is not a string for an Integer. 如果Cost不是整数的字符串,则Cost = int(Cost)将引发ValueError。

as such, 因此,

    while True:
        Cost = raw_input("What was the cost? ")
        try:
             Cost = int(Cost)
             break
        except ValueError:
             print("Please enter an Integer for the cost")

as you can see, break will only be executed if the ValueError is not raised. 如您所见,break仅在没有引发ValueError的情况下执行。

You should not do this though. 但是您不应该这样做。 What you should do is test for isdigit before casting: 您应该做的是在投射前测试isdigit:

    while True:
        Cost = raw_input("What was the cost? ")
        if Cost.isdigit():
             Cost = int(Cost)
             break
        else:
             print("Please enter an Integer for the cost")

exceptions make control flow be unobvious and should be avoided if possible. 异常使控制流程不明显,应尽可能避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM