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.
#2
(Jun-17-2019, 12:46 PM)scotty501 Wrote: What if you wanted to force the input to be a number, and the same again, if not, error message and start again?
https://python-forum.io/Thread-Validating-User-Input

(Jun-17-2019, 12:46 PM)scotty501 Wrote: Lastly, can you make the response ignore the capitalisation - so "Multiply" or "multiply" is acceptable?
str.lower() will change your string to all lowercase. Then you only have to compare the lowercase .

(Jun-17-2019, 12:46 PM)scotty501 Wrote: how would you make it
I would use dictionaries, not a massive if/elif condition
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?"))

calculate = {
    '+':number1 + number2,
    '-':number1 - number2,
}

print(calculate[operator])
Recommended Tutorials:
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 402 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  difference between forms of input a list to function akbarza 6 1,046 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  user input values into list of lists tauros73 3 1,077 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 2,346 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,519 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,794 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 5,093 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 1,978 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  taking input doesnt print as list bntayfur 2 2,131 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  python library not defined in user defined function johnEmScott 2 3,885 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