我是Python的新手,在输入“n”或“N”后,在表格中打印信息时遇到问题。代码如下。我可能遗漏了一些简单但却无法解决的问题。任何帮助表示赞赏。
结果如下。如您所见,即使在打印N之后,它仍继续要求速度输入:
以MPH.50输入速度 输入以小时计的行程时间 你还有另一个计算输入吗? (输入y表示是,或输入N表示否:)y 以MPH.60输入速度 输入以小时计的行程时间 你还有另一个计算输入吗? (输入y表示是,或输入N表示否:)y 以MPH.75输入速度 输入以小时计的行程时间 你还有另一个计算输入吗? (输入y表示是,或输入N表示否:)n 以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():
"""
"""'
答案 0 :(得分:0)
请注意代码附带了许多缩进错误。说过你至少应该纠正
while another == 'y' or 'Y'
到
while another == 'y' or another == 'Y'
确实or 'Y'
始终评估为True