Python Forum
Forcing input from pre-defined list.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Forcing input from pre-defined list.
#1
Hello All,

I am stumbling into the world of programming, and have started by watching videos, and reading beginner instructions on Python. I have just started with Hello World, and using an online lessons, a basic calculator script. I have ventured off and wondering if I can make it a little more functional and what does what.


Below is the code, the question is, how would you make it, that the user has to select the pre-defined options, and if not, it gives an error message like, try again selecting from he above options? Right now the script will run to the end until the error comes back. What if you wanted to force the input to be a number, and the same again, if not, error message and start again?

Lastly, can you make the response ignore the capitalisation - so "Multiply" or "multiply" is acceptable?

I do appreciate this is basic stuff, so be kind :)

number1 = int(input("What is the first number?"))
operator = input("What do you want to do + Plus - Minus * Multiply / Divide or p Power of? ")
number2 = int(input("What is the second number?"))

if operator == "+":
    answer = number1+number2
elif operator == "-":
    answer = number1-number2
elif operator == "*":
    answer = number1*number2
elif operator == "/":
    answer = number1/number2
if operator == "Plus":
    answer = number1+number2
elif operator == "Minus":
    answer = number1-number2
elif operator == "Multiply":
    answer = number1*number2
elif operator == "Divide":
    answer = number1/number2
elif operator == "p":
    answer = number1**number2
elif operator == "power of":
    answer = number1**number2

print("The answer is " + str(answer))
Reply


Messages In This Thread
Forcing input from pre-defined list. - by scotty501 - Jun-17-2019, 12:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 438 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  difference between forms of input a list to function akbarza 6 1,109 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  user input values into list of lists tauros73 3 1,102 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 2,361 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,594 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,836 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 5,169 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 2,003 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  taking input doesnt print as list bntayfur 2 2,152 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  python library not defined in user defined function johnEmScott 2 3,912 May-30-2020, 04:14 AM
Last Post: DT2000

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020