Python Forum
how to make sure an input is from the wanted type
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to make sure an input is from the wanted type
#1
hey guys,
so as a continuation of the previous thread - and taking my own code (as it is easier for me to comprehend) , how do you make sure an input is from the desired type ? (in this case - integer),
suppose the type is a different kind - how is it being accompanied with a message (suppose - "please enter an integer kind of number") and re-asking for an input...

base code:

import random

RNumber = random.randint(1,10)

user_input = int(input("please guess a number between 0 and 10: \n"))

while user_input != RNumber:
    if user_input > RNumber:
        user_input = int(input("choose a lower number: \n"))
    elif user_input < RNumber:
        user_input = int(input("choose a higher number: \n"))

print("the number you chose is correct (", RNumber, ")")
Reply


Messages In This Thread
how to make sure an input is from the wanted type - by astral_travel - Oct-26-2022, 05:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to make input come after input if certain line inserted and if not runs OtherCode Adrian_L 6 3,373 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Make the answer of input int and str enderfran2006 2 2,040 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  How to make input goto a different line mxl671 2 2,493 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Type hinting - return type based on parameter micseydel 2 2,532 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,792 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  catch input type error mcmxl22 5 3,120 Aug-11-2019, 07:33 AM
Last Post: wavic
  Getting type from input() function in Python 3.0 leodavinci1990 7 3,849 Jul-29-2019, 08:28 PM
Last Post: avorane
  how i can check the input type? Firdaos 3 2,897 Dec-13-2018, 11:39 PM
Last Post: wavic
  How to make an input trigger the output once no matter how long input is high cam2363 3 3,296 Feb-05-2018, 01:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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