python - while循环在Python中连续运行,输入好还是不好?

标签 python input error-handling while-loop break

我正在编写一个代码,要求用户提供一个百分比,并一直询问用户是否输入了可接受的输入。但是,当我运行此命令时,无论我输入哪种输入,while循环都不会中断。

这是我的代码:

import math

while True:
    try:
        entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
    except ValueError:
        print("Sorry, an acceptable input was not entered. Try again.")
        continue

    if entered > 100:
        print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
        continue
    elif entered <= 0:
        print("Sorry, a percent cannot be negative. Try again.")
        continue
    else:
        #the percent entered is valid, break out of while loop
        break

print("Ship is traveling at ", entered, "% the speed of light.")
print("  ")

speedOfLight = 299792458                         #speed of light constant
percentage = entered / 100                       #turn entered percent into decimal
speed = speedOfLight * percentage                #actual speed (in m/s)         
denominator = math.sqrt(1 - (percentage ** 2))   #denominator of factor equation
factor =  1 / denominator                        #solve for given factor equation

shipWeight = 70000 * factor                      #given ship weight * factor
alphaCentauri = 4.3 / factor                     # given times divided by the factor
barnardsStar = 6.0 / factor
betelgeuse = 309 /factor
andromeda = 2000000 / factor

print("At this speed: ")
print("    Weight of the shuttle is ", shipWeight)
print("    Perceived time to travel to Alpha Centauri is ", alphaCentauri, " years.")
print("    Perceived time to travel to Barnard's Star is ", barnardsStar, " years.")
print("    Perceived time to travel to Betelgeuse is ", betelgeuse, " years.")
print("    Perceived time to travel to Andromeda Galaxy is ", andromeda, " years.")

最佳答案

您的缩进有点奇怪,并且代码从未到达break语句,因为在此之前您对循环进行了continue。幸运的是,您可以使用and else关键字使其工作:

while True:
    try:
        entered = float(raw_input("Please enter the velocity (as a percentage of the speed of light): "))
    except ValueError:
        print("Sorry, an acceptable input was not entered. Try again.")
        continue
    else: # no exception
        if entered > 100:
            print("Sorry, a velocity greater than the speed of light cannot be used. Try again.")
            continue
        elif entered <= 0:
            print("Sorry, a percent cannot be negative. Try again.")
            continue
        else:
            #the percent entered is valid, break out of while loop
            break

关于python - while循环在Python中连续运行,输入好还是不好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36323373/

相关文章:

python - 在 Python 中处理具有未知默认值的类属性

python - 安装requirements.txt错误,语法错误-python2

ruby-on-rails - 使用 form_tag 时传递错误消息?

javascript - 在 Facebook API 中输入错误代码 191 - 我做错了什么?

python - 我怎样才能一次 struct.unpack 多个数字

python - Tensorflow 和 OpenCV 实时分类

java - 当我从用户那里获取输入时,为什么我的 java 代码会抛出错误?

c - 数组内容无法正确显示

html - 输入的 CSS 流体布局

wcf - WCF : How to handle errors when calling another WCF service?