Looping program based on user string input not gett

Related searches

I need this program to do one of two things from my repeat function, but it should be able to do both and I can't work out why. It must do 2 things

  • make it rerun my main() subprogram,
  • or say "Goodbye." and close the entire program, whichever one comes first.

I have tried making it just an if-else statement rather than if-elif-else, which didn't change anything, I have also tried rearranging the code, but that only changes the single output I can get from the subprogram. This is the subprogram currently:

def repeatloop():
  repeat = input("Do you want to calculate another bill? (y/n): ")
if repeat == 'n' or 'N':
  print("Goodbye.")
  time.sleep(2)
  sys.exit()
elif repeat == 'y' or 'Y':
  main()
else:
  print("Error. Program will shut down.")
  time.sleep(2)
  sys.exit()

It should be able to repeat the program (based on the y or Y input), terminate the program and display "Goodbye." based on the n or N input or it should display the "Error. Program will shut down." message before closing if an invalid input is entered. Many thanks to whoever can help me!

You may use in () for the comparison with a list. It compares the value of the variable with the list.

def repeatloop():
  repeat = input("Do you want to calculate another bill? (y/n): ")
  if repeat in ('n','N'):
    print("Goodbye.")
    time.sleep(2)
    sys.exit()
  elif repeat in ('y','Y'):
    main()
  else:
    print("Error. Program will shut down.")
    time.sleep(2)
    sys.exit()

Java while loop, In Java, a while loop is used to execute statement(s) until a condition is true. Following program asks a user to input an integer and prints it until the user enter 0 (zero). public static void main(String[] args) { int n; Get Input From User If the user gives an invalid input, (any other character or letter not A-F), the program will loop, asking for valid input until it's given. After the program runs all the way through, and the user gets their results, the user clears the screen by hitting enter and the program starts again from the beginning. Here's the code I have so far:

if repeat == 'n' or 'N' doesn't work, since you are checking bool('N') which is always true, the same holds true for repeat == 'y' or 'Y', since bool('Y') is always true.

You start by checking if repeat is in the list ['n','N'], or is not in the list ['y','Y'], then you exit the program, else you call the same function repeatloop

import time, sys

def repeatloop():
    repeat = input("Do you want to calculate another bill? (y/Y/n/N): ")
    if repeat in ['n', 'N']:
        print("Goodbye.")
        time.sleep(2)
        sys.exit()
    elif repeat in ['y', 'Y']:
       repeatloop()
    else:
        print("Error. Program will shut down.")
        time.sleep(2)
        sys.exit()

repeatloop()

While Loops and Input, World � Variables, Strings, and Numbers � Lists and Tuples � Introducing Functions While loops are really useful because they let your program run until a user This section also introduces the first way to get input from your program's users. The function input() will work in Python 2.7, but it's not good practice to use it. How to take String input in Java Java nextLine() method. The nextLine() method of Scanner class is used to take a string from the user. It is defined in java.util.Scanner class. The nextLine() method reads the text until the end of the line.

Hello and welcome to Stackoverflow. Your problem is that your if statements are not exactly correct; and also your whole function is not correctly indented.

if repeat == 'n' or 'N' is different from if repeat == 'n' or repeat == 'N'.

In the second case, you test two different statements on the repeat keyword, where on the first one you're seeing whether:

  1. repeat is equal to n, and also
  2. 'N' is not None; which isn't & this case always returns true.

Another way to do it could be if repeat in ["n", "N"] or even better: if repeat.lower() == 'n'

So put it all together, your code should be refined to:

def repeatloop():
    repeat = input("Do you want to calculate another bill? (y/n):")
    if repeat.lower() == 'n':
        print("Goodbye.")
        time.sleep(2)
        sys.exit()
    elif repeat.lower() == 'y':
        main()
    else:
        print("Error. Program will shuts down.")
        time.sleep(2)
        sys.exit()

How to Indefinitely Request User Input Until Valid in Python, User input is a tricky thing. I learned very early on that just because you prompt the user's “email address” or “age”, you're not going to get a� The value entered by the user is stored in the variable num. Suppose, the user entered 10. The count is initialized to 1 and the test expression is evaluated. Since the test expression count<=num (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1.

Loops and Strings | Think Java, In this chapter, you'll learn how to use while and for loops to add repetition to your code. there is no obvious proof that n will ever reach 1 and that the program will ever terminate. For example, when validating user input as in Section 5.9, it's impossible to To get the last character, you have to subtract 1 from length :� Get User Input. You have already learned that Console.WriteLine() is used to output (print) values. Now we will use Console.ReadLine() to get user input.. In the following example, the user can input his or hers username, which is stored in the variable userName.

Do While Loop in Java, The do while loop is similar to the while loop with an important difference: the do Scanner; // needed for Scanner Class /** * This program demonstrate do while loop. main(String[] args) { int value; // to hold data entered by the user int sum = 0; char choice; // to hold 'y' or 'n' // Create a Scanner object for keyboard input. This means that if the user inputs the string password, then the loop will stop and the program will continue to execute any code outside of the loop. However, if the string that the user inputs is not equal to the string password, the loop will continue. Next, we’ll add the block of code that does something within the while loop:

Next: This program tries to multiply an integer value received by the user by 10 and display the product. Line: The string "line" is assigned to the reference of the string data allocated by Console.ReadLine and filled with the input.

Comments
  • Is your indentation correct?
  • Possible duplicate of How to test multiple variables against a value? Also see stackoverflow.com/q/20002503
  • any(["n", "N"]) == repeat won't give you the right answer either. You would be getting True == repeat which is always False.