[英]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
:如果您只想运行特定次数,则可以使用具有
range
的for
循环:
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.