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.
#11
Hi,

 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
Reply
#12
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)}')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 383 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  difference between forms of input a list to function akbarza 6 1,036 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 2,337 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,482 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,779 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 5,072 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,971 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  taking input doesnt print as list bntayfur 2 2,120 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  python library not defined in user defined function johnEmScott 2 3,870 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