简体   繁体   English

为什么我的程序菜单卡在无限循环中?

[英]Why is the menu of my program stuck in an infinite loop?

I'm creating a program so that a track coach can easily pull up runners times as well as input them.我正在创建一个程序,以便田径教练可以轻松地拉起跑步者的时间并输入它们。

I'm trying to figure out why when I start my program, it runs the function 'MENU', looping it.我试图弄清楚为什么当我启动我的程序时,它运行 function 'MENU',循环它。

user_input = 0

print('MENU')
print('1 - Add runner data to file')
print('2 - Display runners and their times')
print('3 - Calculate the average run time')
print('4 - Display the fastest time')
print('5 - EXIT')
print()

def MENU():
    user_input = int(input('Enter your Menu choice >> '))
    return -1

def DATA(f_runner, f_time):
    f_runner = str(input('Enter runners name >> '))
    f_time = str(input('Enter the runners time in hours >> '))
    print('Runners data entered into the file.')
    f = open('myFile.txt', 'w')
    f.write(str(f_runner))
    f.write(str(f_time))
    f.close()
    return f_runner, f_time

def DISPLAY():
    contents = f.readlines()
    f = open('myFile.txt')
    print(contents)

runners_data = 0
runner = 0
runner_time = 0
average_time = 0
file_runner = ''
file_time = 0.0
contents = ''
program_exit = False
menu_start = 0

while program_exit == False:
    menu_start = MENU()
    while user_input > 0 and user_input < 6:
        if user_input == '1':
            DATA(file_runner, file_time)
        elif user_input == '2':
            Display()
        elif user_input == '5':
            program_exit = True

You are returning -1 instead of user_input in MENU()您在 MENU() 中返回 -1 而不是 user_input

Alongside what Samraj said, I believe it's also because your if statement is comparing if the user input is returned as a string, when you're expecting the user to return an int.除了 Samraj 所说的之外,我相信这也是因为您的 if 语句正在比较用户输入是否作为字符串返回,而您希望用户返回一个 int。 You could just remove it from the bottom and have it run inside menu and just call MENU().你可以把它从底部移除,让它在菜单中运行,然后调用 MENU()。

Try this and edit it to what you need试试这个并将其编辑为您需要的内容

print('MENU')
print('1 - Add runner data to file')
print('2 - Display runners and their times')
print('3 - Calculate the average run time')
print('4 - Display the fastest time')
print('5 - EXIT')
print()

def MENU():
    the_input = int(input('Enter your Menu choice >> '))
    if the_input == 1:
        print("hello")
        DATA(file_runner, file_time)
    elif the_input == 2:
        Display()
    elif the_input == 5:
        exit()
    return the_input

def DATA(f_runner, f_time):
    f_runner = str(input('Enter runners name >> '))
    f_time = str(input('Enter the runners time in hours >> '))
    print('Runners data entered into the file.')
    f = open('myFile.txt', 'w')
    f.write(str(f_runner))
    f.write(str(f_time))
    f.close()
    return f_runner, f_time

def DISPLAY():
    contents = f.readlines()
    f = open('myFile.txt')
    print(contents)

runners_data = 0
runner = 0
runner_time = 0
average_time = 0
file_runner = ''
file_time = 0.0
contents = ''
program_exit = False
menu_start = 0

MENU()

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

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