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
因此,在這一行中,它不會顯示您希望查看更多內容,而是顯示“輸入您的姓名”。我不確定是否應該洗掉此代碼或更改某些內容
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!")
uj5u.com熱心網友回復:
您可以按如下方式更改代碼:
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”以外的任何內容,回圈將中斷。
uj5u.com熱心網友回復:
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
取消縮進所有這些一次。我建議先進行除錯。
uj5u.com熱心網友回復:
如果用戶不想繼續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!")
uj5u.com熱心網友回復:
您需要一種機制來重復輸入,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
uj5u.com熱心網友回復:
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() 行來改進代碼,從而減少代碼的長度。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/335381.html
