所以我正在制作一个程序,可以根据需要多次重复向用户打招呼,但是当他们想要继续时,它不会显示该代码

Posted

技术标签:

【中文标题】所以我正在制作一个程序,可以根据需要多次重复向用户打招呼,但是当他们想要继续时,它不会显示该代码【英文标题】: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 【发布时间】:2021-12-10 11:59:45 【问题描述】:
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!")

【问题讨论】:

你永远不会在你的循环中改变play。那么您如何期望while play == "y":while play == "y": 退出循环?当然while play == "n": 永远不会循环,因为它处于一个循环中,程序只有在play'y' 时才会进入。 【参考方案1】:
        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

取消缩进一次。我建议先进行调试。

【讨论】:

【参考方案2】:

您可以按如下方式更改您的代码:

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”,循环就会中断。

【讨论】:

【参考方案3】:

您需要一种机制来重复输入,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

【讨论】:

但在开始时我想问用户是否要在“此程序将打印...”之后继续 @Naomi - 我已更新程序以满足此要求。 非常感谢!【参考方案4】:

如果用户不想继续,请使用 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!")

【讨论】:

【参考方案5】:
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() 行,从而减少代码的长度来改进代码。

【讨论】:

以上是关于所以我正在制作一个程序,可以根据需要多次重复向用户打招呼,但是当他们想要继续时,它不会显示该代码的主要内容,如果未能解决你的问题,请参考以下文章

如何制作具有不同内容的重复通知

当宽度根据百分比动态变化时,制作一个 <div> 正方形 [重复]

在 MailChimp 中多次发送活动

根据单元格值向用户窗体添加复选框

在php中停止不推荐使用的警告[重复]

捕获屏幕的区域[重复]