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.

 Thanks - that makes sense for me to replace the "if and elif" commands, but apart from keeping the code smaller, does it matter?
Yes, it is more pythonic and more elegant and more readable.

Regards, noisefloor
Thank you both - brilliantly explained and actually made a lot of sense :)

I am starting (slowly) to get my head around coding, and you patience is appreciated.

On this the last statement, there is an error I believe as "first, second" is not defined?

first, second = validate('Enter first number: '), validate('Enter second number: ')
op = operation(f"Enter operation (one from: {', '.join(ops.keys())}): ")   # ensures that only defined operators are listed
print(f'Answer is {op(first, second)}')

I solved the problem myself with some trial and error :)

It passes the pep8 approval too ;) Getting better for a newbie.

first = validate('Enter first number: ')
op = operation(f"Enter operation (one from: {', '.join(ops.keys())}): ")
second = validate('Enter second number: ')
print(f'Answer is {op(first, second)}')

Possibly Related Threads…
Thread Author Replies Views Last Post
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 351 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 545 Aug-21-2020, 09:20 PM
Last Post: Marbelous
  Function to return list of all the INDEX values of a defined ndarray? pjfarley3 2 339 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  taking input doesnt print as list bntayfur 2 354 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  python library not defined in user defined function johnEmScott 2 425 May-30-2020, 04:14 AM
Last Post: DT2000
  list from defined function Coastal 3 634 Jan-06-2020, 10:20 AM
Last Post: perfringo
  what would you call the input for the parameter(s) of a function you have defined? rix 3 529 Dec-16-2019, 12:04 AM
Last Post: rix
  Help with calling list from user input farispython 5 729 Nov-03-2019, 03:13 PM
Last Post: Gribouillis
  Find Average of User Input Defined number of Scores DustinKlent 1 512 Oct-25-2019, 12:40 AM
Last Post: Larz60+
  with input remove a string from the list konsular 3 593 Oct-12-2019, 09:25 AM
Last Post: konsular

Forum Jump:

User Panel Messages

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