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
#4
You could use a try block.

import random

RNumber = random.randint(1, 10)

while True:
    try:
        user_input = int(input('Guess a number\n>> '))
    except ValueError:
        print('Please use only whole numbers')
        continue
    else:
        if user_input > RNumber:
            print('Number is to high')
        elif user_input < RNumber:
            print('Number is too low')
        else:
            print(f'Congradulation! You guessed the number {RNumber}')
            RNumber = random.randint(1,10)
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Messages In This Thread
RE: how to make sure an input is from the wanted type - by menator01 - Oct-26-2022, 06:49 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,437 Apr-04-2021, 06:10 PM
Last Post: Adrian_L
  Make the answer of input int and str enderfran2006 2 2,083 Oct-12-2020, 09:44 AM
Last Post: DeaD_EyE
  How to make input goto a different line mxl671 2 2,543 Feb-04-2020, 07:12 PM
Last Post: Marbelous
  Type hinting - return type based on parameter micseydel 2 2,577 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,830 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  catch input type error mcmxl22 5 3,206 Aug-11-2019, 07:33 AM
Last Post: wavic
  Getting type from input() function in Python 3.0 leodavinci1990 7 3,910 Jul-29-2019, 08:28 PM
Last Post: avorane
  how i can check the input type? Firdaos 3 2,929 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,348 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