Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to manage an input()
#1
Hello, I am new to Python and was trying this code (which is used to guess the number picked by system -using the random function-) and I wanted to add some features, such as:

If the number picked by user is higher than the one picked by the random function, print "the number you picked is lower", and the same thing if it's lower (print: "it's higher").


* To be clear: I want to save (a variable) the input throught "stdin"(of type int - I know I have to cast it from str to int-), and then use this variable to keep referencing the user input to improve my code and do what I explained on the 2nd paragraph.


This is the orignal code (working ok, but with no features):

import random
secret = random.randint(0,50)

message = 'Pick a number from 1 to 49: '

even_number = (secret % 2) == 0

if even:
    print("The number is even")
else:
    print("The number is odd")

attempts = 0
MAX = 5

while int(input(message)) != secret:

    if attempts >= MAX -1:
        print("You loose")
        break
    else:
        attempts += 1
        print('No!')
else:
    print('You guessed!')
Thank you in advance!!
Reply
#2
Something like this will get you pointed in right direction (the first part is your code)
import random
secret = random.randint(0,50)
 
message = 'Pick a number from 1 to 49: '
 
even_number = (secret % 2) == 0
 
if even:
    print("The number is even")
else:
    print("The number is odd")
 
attempts = 0
MAX = 5
found = False  
while not found:
    guess = int(input('Enter a guess: '))
    if guess == secret:
        blah
    elif guess > secret:
        blah
    elif guess < secret:
        blah
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python manage variables across project level mg24 1 913 Nov-12-2022, 05:01 AM
Last Post: deanhystad
  How do you manage script? kucingkembar 14 2,711 Oct-15-2022, 06:32 PM
Last Post: Gribouillis
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,595 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  how to manage crypto trading flooding data from exchange servers Mikeardy 0 1,232 Dec-26-2021, 08:31 PM
Last Post: Mikeardy
  API Gateway to manage multiple API's get put data robsuttonjr 2 2,528 Mar-09-2021, 04:09 PM
Last Post: robsuttonjr
  Manage user names in TRAC susja 3 5,933 Dec-06-2020, 09:34 PM
Last Post: susja
  How to manage multiple datasets in Python ThePhantom 2 1,917 May-06-2020, 12:17 AM
Last Post: ThePhantom
  Easier way to manage dependencies? Tylersuard 2 2,350 Jan-01-2020, 09:19 PM
Last Post: Tylersuard

Forum Jump:

User Panel Messages

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