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
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
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