所以我正在制作一个程序,可以根据需要多次向用户重复问候语,但是当他们想继续时,它不会显示该代码
So I'm making a program that repeats a greeting to the user as many times they want, but when they want to continue it doesnt show that code
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
while play == "n":
continue
所以在这一行中它没有显示 would you like to see more,而是显示 Enter your name。
我不确定是否应该删除此代码或更改某些内容
play_2 = input("Would you like to see more? (y/n): ")
while play_2 == "y":
name_2 = input("Enter your name: ")
print()
greetings_2 = int(input("How many times would you like to see your custom
greeting?: "))
print()
for i in range(greetings_2):
print("Hello " + name_2 + "." + "It's a pleasure to meet you!")
while play_2 == "n":
continue
print()
print("Bye. Thanks for using my program!")
play_2 = input("Would you like to see more? (y/n): ")
while play_2 == "y":
name_2 = input("Enter your name: ")
print()
greetings_2 = int(input("How many times would you like to see your custom
greeting?: "))
print()
for i in range(greetings_2):
print("Hello " + name_2 + "." + "It's a pleasure to meet you!")
while play_2 == "n":
continue
取消缩进一次。我建议先进行调试。
您可以按如下方式更改代码:
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
play = input("Would you like to see more? (y/n): ")
问题出在 while 循环的设置上。如果用户想在最后再次 运行 该程序,则只需要一个检查。如果用户输入除“y”以外的任何内容,循环将中断。
您需要一种重复输入的机制,Do you want to continue? (y/n):
例如在下面的代码中,它已被放入 while
循环中,因此它会在打印给定名称指定次数后再次 运行。
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
greetings = int(input("How many times would you like to see your custom greeting?: "))
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
play = input("Do you want to continue? (y/n): ")
样本运行:
This program will print the amount of greeting you would like to see.
Do you want to continue? (y/n): y
Enter your name: Naomi
How many times would you like to see your custom greeting?: 2
Hello Naomi. It's a pleasure to meet you!
Hello Naomi. It's a pleasure to meet you!
Do you want to continue? (y/n): y
Enter your name: Campbell
How many times would you like to see your custom greeting?: 2
Hello Campbell. It's a pleasure to meet you!
Hello Campbell. It's a pleasure to meet you!
Do you want to continue? (y/n): n
Process finished with exit code 0
使用 while True
的无限循环,如果用户不想继续,则使用 break
离开。这样做的好处是您不必在多行代码中跟踪额外的变量。
print("This program will print the amount of greetings you would like to see.")
while True:
name = input("Enter your name: ")
number_of_greetings = int(input("How many times would you like to see your custom greeting?: "))
for _ in range(number_of_greetings):
print(f"Hello {name}. It's a pleasure to meet you!")
play_again = input("Do you want to continue? (y/n): ")
if play_again != 'y':
break
print("Bye. Thanks for using my program!")
print("This program will print the amount of greeting you would like to see.")
while (play := input("\nDo you want to continue? (y/n): ")).lower() == "y" :
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
您可以通过对输入添加检查并在字符串末尾使用 \n 来消除 print() 行从而减少代码的长度来改进代码。
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
while play == "n":
continue
所以在这一行中它没有显示 would you like to see more,而是显示 Enter your name。 我不确定是否应该删除此代码或更改某些内容
play_2 = input("Would you like to see more? (y/n): ")
while play_2 == "y":
name_2 = input("Enter your name: ")
print()
greetings_2 = int(input("How many times would you like to see your custom
greeting?: "))
print()
for i in range(greetings_2):
print("Hello " + name_2 + "." + "It's a pleasure to meet you!")
while play_2 == "n":
continue
print()
print("Bye. Thanks for using my program!")
play_2 = input("Would you like to see more? (y/n): ")
while play_2 == "y":
name_2 = input("Enter your name: ")
print()
greetings_2 = int(input("How many times would you like to see your custom
greeting?: "))
print()
for i in range(greetings_2):
print("Hello " + name_2 + "." + "It's a pleasure to meet you!")
while play_2 == "n":
continue
取消缩进一次。我建议先进行调试。
您可以按如下方式更改代码:
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
play = input("Would you like to see more? (y/n): ")
问题出在 while 循环的设置上。如果用户想在最后再次 运行 该程序,则只需要一个检查。如果用户输入除“y”以外的任何内容,循环将中断。
您需要一种重复输入的机制,Do you want to continue? (y/n):
例如在下面的代码中,它已被放入 while
循环中,因此它会在打印给定名称指定次数后再次 运行。
print("This program will print the amount of greeting you would like to see.")
print()
play = input("Do you want to continue? (y/n): ")
while play == "y":
name = input("Enter your name: ")
greetings = int(input("How many times would you like to see your custom greeting?: "))
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
play = input("Do you want to continue? (y/n): ")
样本运行:
This program will print the amount of greeting you would like to see.
Do you want to continue? (y/n): y
Enter your name: Naomi
How many times would you like to see your custom greeting?: 2
Hello Naomi. It's a pleasure to meet you!
Hello Naomi. It's a pleasure to meet you!
Do you want to continue? (y/n): y
Enter your name: Campbell
How many times would you like to see your custom greeting?: 2
Hello Campbell. It's a pleasure to meet you!
Hello Campbell. It's a pleasure to meet you!
Do you want to continue? (y/n): n
Process finished with exit code 0
使用 while True
的无限循环,如果用户不想继续,则使用 break
离开。这样做的好处是您不必在多行代码中跟踪额外的变量。
print("This program will print the amount of greetings you would like to see.")
while True:
name = input("Enter your name: ")
number_of_greetings = int(input("How many times would you like to see your custom greeting?: "))
for _ in range(number_of_greetings):
print(f"Hello {name}. It's a pleasure to meet you!")
play_again = input("Do you want to continue? (y/n): ")
if play_again != 'y':
break
print("Bye. Thanks for using my program!")
print("This program will print the amount of greeting you would like to see.")
while (play := input("\nDo you want to continue? (y/n): ")).lower() == "y" :
name = input("Enter your name: ")
print()
greetings = int(input("How many times would you like to see your custom greeting?: "))
print()
for i in range(greetings):
print("Hello " + name + "." + " It's a pleasure to meet you!")
您可以通过对输入添加检查并在字符串末尾使用 \n 来消除 print() 行从而减少代码的长度来改进代码。