首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python 3新手-尝试将结果打印到表格中

Python 3新手-尝试将结果打印到表格中
EN

Stack Overflow用户
提问于 2016-10-13 06:11:41
回答 1查看 74关注 0票数 0

我是Python的新手,一旦输入'n‘或’N‘,我就遇到了在表格中打印信息的问题。代码如下。我可能遗漏了一些简单的东西,但还没能弄明白。任何帮助都是非常感谢的。

结果如下所示。如您所见,即使在打印了N之后,它也会继续要求输入速度:

输入以MPH.50为单位的速度。输入以小时为单位的时间。3您是否还有其他计算要输入?(输入y表示是,输入N表示否:)y输入以MPH.60表示的速度。60输入以小时为单位的时间。4是否要输入其他计算?(输入y表示是,输入N表示否:)y输入以MPH.75表示的速度,输入以小时为单位的时间。3是否要输入其他计算?(输入y表示是,输入N表示否:)n输入速度,单位为MPH。

‘#这个程序将使用速度(英里/小时)和行驶小时数来计算车辆行驶的距离#。

代码语言:javascript
复制
# 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():
"""
"""'
EN

回答 1

Stack Overflow用户

发布于 2016-10-13 06:20:23

注意代码中有很多缩进错误。话虽如此,你至少应该改正

代码语言:javascript
复制
while another == 'y' or 'Y'

代码语言:javascript
复制
while another == 'y' or another == 'Y'

实际上,or 'Y'的计算结果始终为True

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40009211

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档