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.
#5
Thanks for all the great responses, I am trying to adapt my sample code as I want to learn what is happening, rather than just copy someone else code. I am playing with the first suggestion and using the sample code below.

while True:
    text = input('Please enter an integer: ')
    if text.isdigit():
        number = int(text)
        break
    print('{} is not an integer.'.format(text))
print((number + 1) * 2)
This breaks my original code as I would expect.

Q1 - Would that be because (int input) will not work with text.isdigit, so the only way I can get the two to play nice is to make it a input not int input, which then of means, my if and elif statement does not work as it does not see it as an integer?
Q2 - How would you fix it?

As I say, I am trying to avoid just copying peoples code, and understand each part first. (Very early days).

My revised code, which does not output integer as below:

while True:
    number1 = input('Please enter the first number: ')
    if number1.isdigit():
        number = int(number1)
        break
    print('{} is not a number! Try again.'.format(number1))

operator = input("What do you want to do + Plus - Minus * Multiply / Divide or p Power of? ")

while True:
    number2 = input('Please enter the second number: ')
    if number2.isdigit():
        number = int(number2)
        break
    print('{} is not a number! Try again.'.format(number2))

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
RE: Forcing input from pre-defined list. - by scotty501 - Jun-18-2019, 08:26 AM

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,108 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,591 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 2,834 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  I need advise with developing a brute forcing script fatjuicypython 11 5,167 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,002 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,910 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