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,

1
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?

1
2
3
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.

1
2
3
4
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
  Brute Forcing Anagrams Anorak 12 1,896 Jan-10-2025, 04:07 PM
Last Post: Anorak
  Forcing matplotlib to NOT use scientific notation when graphing sawtooth500 4 4,872 Mar-25-2024, 03:00 AM
Last Post: sawtooth500
  difference between forms of input a list to function akbarza 6 2,334 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  user input values into list of lists tauros73 3 1,944 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 2,912 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  Group List Elements according to the Input with the order of binary combination quest_ 19 9,173 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 3,967 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 8,348 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,652 Jul-10-2020, 04:51 AM
Last Post: pjfarley3
  taking input doesnt print as list bntayfur 2 2,747 Jun-04-2020, 02:48 AM
Last Post: bntayfur

Forum Jump:

User Panel Messages

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