-2

Hello guys I wondered If you could please help me with my python code for my school, I basically do not understand how to end the code when the user inputs an answer that does not fit the criteria for it to end, and do not know how to give out a retype message when he/she writes something like just pressing enter enter

operation = int(input("how long has your company been operating for?: "))
if operation > 5:
    print("Wow your company has gone on for quite some time now, huh?")

else:
    print("Sorry your company does not fit into our criteria")

Such as here, if the user inputs 4 the program says "sorry your company doesn't meet the requirements" but dosnt end the code it juts carries on to the next program, and if the user presses enter or types in letters it will give me back a trace back error when I want it to just repeat the question and say "please type an appropriate answer"

Employees = int(input("How many employees are currently working in your company?"))
if Employees > 1000:
    print("Great to see your company is growing")
else:
    print("Sorry you must have at least 1000 employees if you want to be our supplier")

This also has the same problem as the last program it carries on when it shouldn't such as number under (1000) and dosnt accept random input by just saying trace back error instead of retyping message and saying "please give appropriate input"

  • 2
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – fractals Aug 20 '18 at 09:28

2 Answers2

2

This should do the trick:

answered = False

while not answered:
   inp = input("how long has your company been operating for?: ")
   try:
       operation = int(inp)
   catch ValueError:
       print("Wrong input, try again")
       continue

   if operation > 5:
       print("Wow your company has gone on for quite some time now, huh?")
       answered = True

   else:
       print("Sorry your company does not fit into our criteria")
       sys.exit()
Sharku
  • 1,052
  • 1
  • 11
  • 24
  • this is giving me an error at the !answered for some reason –  Aug 20 '18 at 09:39
  • sorry I did a little mistake pythonic way is while not answered – Sharku Aug 20 '18 at 09:45
  • for some reason the code still continues when input is under 5 and when random input is given such as letters it gives me trace back error instead of repeating the code –  Aug 20 '18 at 09:58
  • You were right. The error has to be catched... my mistake – Sharku Aug 20 '18 at 10:16
1
import sys

while True:
    operation = input("how long has your company been operating for?: ")
    if operation.isdigit():
        operation = int(operation)
        if operation > 5:
            print("Wow your company has gone on for quite some time now, huh?")
            break
        else:
            print("Sorry your company does not fit into our criteria")
            exit()
    else:
        print("please enter a number")
Stef van der Zon
  • 633
  • 4
  • 13
  • This works but how do I make it when the user types in random leters or just presses enter it rrepeats the messege? –  Aug 20 '18 at 09:41
  • Not sure what you mean by repeats the message? – Stef van der Zon Aug 20 '18 at 09:43
  • So if the user writes a random response such as letters or presses enter I am getting a traceback error highlighted in red in the instructions it says our code should counteract that and tell the user to type in a valid input and keep repeating this untill he does it –  Aug 20 '18 at 09:46
  • This code should not give any traceback errors. The code tries to make an integer out of the user input. If that is not possible (user entered a letter or nothing) it will start over after executing the except code. – Stef van der Zon Aug 20 '18 at 09:51
  • im sorry to keep asking and being annoying, but this keeps asking the user to write in a valid response which we want, but now no longer stops it from continuing when he has inputted less than 5 years. –  Aug 20 '18 at 09:53
  • Is it ok like this? – Stef van der Zon Aug 20 '18 at 10:34