【发布时间】:2016-10-12 22:11:40
【问题描述】:
我是 Python 新手,在输入“n”或“N”后,在获取要在表格中打印的信息时遇到问题。代码如下。我可能遗漏了一些简单的东西,但无法弄清楚。任何帮助表示赞赏。
这是结果。如您所见,即使在打印 N 之后,它仍然继续要求输入速度:
以 MPH.50 为单位输入速度 输入以小时为单位的旅行时间。3 您还有其他计算要输入吗? (输入 y 表示是或输入 N 表示否:)y 以 MPH.60 输入速度 输入以小时为单位的旅行时间。4 您还有其他计算要输入吗? (输入 y 表示是或输入 N 表示否:)y 在 MPH.75 中输入速度 输入以小时为单位的旅行时间。3 您还有其他计算要输入吗? (输入 y 表示是或输入 N 表示否:)n 以 MPH 为单位输入速度。
' # 该程序将计算车辆行驶的距离 # 以英里为单位,使用速度 (mph) 和行驶小时数。
# Create a variable to represent the maxium travel time in hours.
max_travel = 9
min_travel = 1
# Create a variable to represent the maximum speed.
max_speed = 120
# Create a variable to represent the minimum speed.
min_speed = 1
#Define a variable to represent continuation of the program
another = ["y", "Y"]
# Create a variable for saved results to be printed in table
results = []
# main module
def main():
# Get the speed in MPH.
speed = int(input('Enter the speed in MPH.'))
# Validate that the speed entered is not out of range.
while speed > max_speed or speed < min_speed:
if speed > max_speed:
print('ERROR: the speed entered must be a lower number between 1 and 120.')
speed = int(input('Enter a lower speed in MPH.'))
if speed < min_speed:
print('ERROR: the speed entered must be a higher number between 1 and 120.')
speed = int(input('Enter a higher speed in MPH.'))
# Ask user to input travel time in hours
travel_time = int(input('Enter the time travelled in hours.'))
# Validate that the time travelled is within the range of 1 to 9 hours.
while travel_time > max_travel or travel_time < min_travel:
if travel_time > max_travel:
print('ERROR: the time must be a lower number between 1 and 9.')
travel_time = int(input('Enter a different time travelled in hours.'))
# Validate that the time travelled is within the range of 1 to 9 hours.
if travel_time < min_travel:
print('ERROR: the time must be a higher number between 1 and 9.')
travel_time = int(input('Enter a different time travelled in hours.'))
# This will cause the loop, with the exception of the first loop,
# to depend on a 'y' or 'Y' entry to enter any additional entries.
first = False
# Calculate again?
another = input('Do you have another calculation to enter? ' + \
'(Enter y for yes or N for no: )')
# This loop will continue until something other
# than 'y' or 'Y' is entered when prompted.
while another == 'y' or 'Y':
main()
# Calculations saved for table
input_tuple =speed, travel_time
# Print the column headings in a table.
# Print the time travelled and the
# result of the distance calculation
print()
print('Hours\t\tDistance')
print('---------------------')
# Print saved entries and calculations in table
if another !='y' or 'Y':
for input_tuple in results:
# Calculate the distance travelled
distance = speed * travel_time
print("'%s'\t\t'%s'" %(travel_time, distance))
# Call the main function.
def main():
"""
"""'
【问题讨论】:
-
缩进只有 4 个空格。不再。不少于。不要使用标签。
标签: python-3.x