Home > front end >  So I'm making a program that repeats a greeting to the user as many times they want, but when t
So I'm making a program that repeats a greeting to the user as many times they want, but when t

Time:10-25

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

So in this line it doesn’t show would you like to see more, instead it shows Enter your name. I'm not sure if I should erase this code or change something

        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!")

CodePudding user response:

You can alter your code as follows:

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): ")

The problem was the set-up with your while loops. Only one is needed with the check if the user wants to run the program again at the end. The loop will break if the user enters anything else than "y".

CodePudding user response:

        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

Un-indent all of this once. I recommend a debug before anything else.

CodePudding user response:

Use an endless loop with while True that you leave with break if the user doesn't want to continue. This has the advantage that you don't have to track an additional variable over several lines of code.

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!")

CodePudding user response:

You need a mechanism to repeat the input, Do you want to continue? (y/n): e.g. in the following code, it has been put inside the while loop and therefore it will run again after printing the given name for the specified number of times.

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): ")

A sample run:

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

CodePudding user response:

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!")

You could improve the code by adding checks on the inputs and using \n at the end of the strings to eliminate the print() lines thus reducing the length of the code.

  • Related