Python code to randomly guess user input number

Question:

I just just finished (jk I was looking at old code and found this so) but for real this is the final code
final update: Everything now good
thanks to this odd number formula.
The sum of n odd numbers is n^2
How did we arrive to this formula

  • First 1 odd number = 1 Sum = 1 = 1^2
  • First 2 odd numbers =1,3 Sum = 4 = 2^2
  • First 3 odd numbers = 1,3,5 Sum = 9 = 3^2
  • First 4 odd numbers =1,3,5,7 Sum = 16 4^2
  • First 5 odd numbers = 1,3,5,7,9 Sum = 25 =5^2
  • Therefore sum of n odd numbers = n^2

import random,os
userI =True
os.system("cls")
def randomOddNumber(a,b):
  a = a // 2
  b = b // 2 - 1
  number = random.randint(a,b)
  number = (number * 2) + 1
  return number
while userI==True:
    userInput = int(input("Please input a 4 digit number: "))
    compNumber = random.randint(1000, 9999)

    count = 0
    while userInput != compNumber:
         
        if (userInput % 2) == 0:

                compNumber= random.randrange(0, 10000, 2)
                count=count+1
        else:
              compNumber = randomOddNumber(0,9999)
              count =count+1
    print("Match was created on the", count, "attempt.")
    ex = False
    while ex == False:
        userAwnser = input("Would you like to play again?: ")
        if userAwnser == "no"or userAwnser=="No":
            userI = False
            ex = True
        elif userAwnser == "yes"or userAwnser=="Yes":
            userI = True
            ex = True
            os.system("cls")
        else:
            print("Error Not a valid awnser")
            ex = False

Answers:

while True:
    master = int(input("enter a 4 digit number: "))
    if 1000 <= master <= 9999:
        break
    print("That's not a 4-digit number.")

guess = 5500
delta = guess // 2
tries = 0
while guess != master and tries < 10:
    tries = tries+1
    nxt = input(f"My guess is {guess}.  Is your number higher or lower? ")
    if nxt[0] == 'h':
        guess += delta
    elif nxt[0] == 'l':
        guess -= delta
    else:
        print("Come on, type 'higher' or 'lower'.")
        continue
    delta //= 2
    
if master == guess:
    print(f"My guess is {guess}.  I got it right in {tries} tries")
else:
    print("I didn't get it in 10 tries.")
Answered By: Tim Roberts

The main issue here is that you are trying to force input() to split your string input into four variables. Even if you wanted to convert it to integers, you would need to use a for loop or equivalent to separate it into a list. Also, having each digit split is redundant for your case, and you could just compare the integer to a 4 digit random integer (ex: randint(1000, 9999). Below is a simpler and more efficient way of doing this:

import random

userInput = int(input("Please input a 4 digit number: "))
compNumber = random.randint(1000, 9999)


count = 0
while userInput != compNumber:
    compNumber = random.randint(1000, 9999)
    count += 1
print("Match was created on the", count, "attempt.")

Please comment if you have any questions!

Note: This does not have any user input validation and will break if given a string and will be in a forever loop if the input is a number greater then 9999 or smaller then 1000

Answered By: Eli Murphy
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.