简体   繁体   English

我如何继续循环我的程序,以便用户可以输入尽可能多的内容?

[英]How do i keep looping the same my program so the user can input as much as they want?

So i'm very new to python, i'm trying to create a program where the user can input a music chord between the range a - g and then receive back the information about that chord eg what notes make that chord.所以我对python非常陌生,我正在尝试创建一个程序,用户可以在其中输入范围 a - g 之间的音乐和弦,然后接收有关该和弦的信息,例如哪些音符构成该和弦。 What i'm stuck with is letting the user find out about another chord without needing to restart the program.我所坚持的是让用户无需重新启动程序即可找到另一个和弦。 So I want to be able to have the input question again once they've done it already.所以我希望能够在他们已经完成后再次输入问题。

I haven't really tried much as I don't know where to start.我没有真正尝试过,因为我不知道从哪里开始。

chord = input('What chord would you like to find out about? (A-G) ')
if chord.upper() == 'D':
    print(f"The D chord is made up of three notes: {d_chord}")
elif chord.upper() == 'G':
    print(f"The G chord is made up of three notes: {g_chord}")

So basically, when the user finishes their conversion, I want it to loop back to the top so they can again without restarting the program所以基本上,当用户完成他们的转换时,我希望它循环回到顶部,这样他们就可以不用重新启动程序了

Wrap your code into an infinite while loop:将您的代码包装成一个无限的while循环:

while True:
    chord = input('What chord would you like to find out about? (A-G) ')
    if chord.upper() == 'D':
        print(f"The D chord is made up of three notes: {d_chord}")
    elif chord.upper() == 'G':
        print(f"The G chord is made up of three notes: {g_chord}")

If you only want to run it a specific number of times, you can use a for loop with a range :如果您只想运行特定次数,则可以使用具有rangefor循环:

for i in range(0,5): # loop will run 5 times
    chord = input('What chord would you like to find out about? (A-G) ')
    if chord.upper() == 'D':
        print(f"The D chord is made up of three notes: {d_chord}")
    elif chord.upper() == 'G':
        print(f"The G chord is made up of three notes: {g_chord}")

暂无
暂无

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

相关问题 在Python中,如果用户输入错误的值,如何使输入验证继续循环 - In Python, How do I make my input validation keep on looping if the user enters an incorrect value 如何防止我的 Python 程序在我的 SQL 服务器上使用过多的 memory? - How do I keep my Python program from using too much memory on my SQL Server? 如何制作程序,以便用户可以指定文件名? - How do I make my program so that the user can specify a file name? 如何将输入保持在相同的屏幕位置? - how do I keep my input at the same screen location? 我的用于计算用户输入语句中的元音数量的程序不会对同一元音进行两次计数。 我该如何解决? - My program for counting how many vowels in a user input sentence doesn't count the same vowel twice. How do i fix this? 如何在python中存储用户信息,以便下次可以继续使用? - How do I store the user information in python so that i can keep using it next time? 如何使用我的电报机器人存储来自用户的输入,然后在需要时获取输入 - How can i store input from a user using my telegram bot and then fetch the input when i want 为什么我的函数会不断循环,我该如何解决? - Why does my function keep looping and how can i fix it? 如何将计算结果存储在 Python 中,这样我的程序就不会对同一事物进行两次计算? - How do I store results of a computation in Python so my program doesn't compute the same thing twice? 我该如何修正我的代码,以便它可以打印出我想要的结果? - How Do I Fix My Code So It Can print() Out The Results I Want?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM