0

So I am coding a simple guessing game in python. I want to make it that you can choose up to what number you want the computer to guess. Starting from one. How do I make it so that the user input of an integer is the last number the computer will guess to? This is the code:

import random

while True:
    print("Welcome to odds on. Up to what number would you like to go to?")
    num = int(input())
    
    
    
    print("Welcome to odds on. Make your choice:")
    choice = int(input())

    cc = [1, num]

    print()

    computerchoice = random.choice(cc)
marcel
  • 11

2 Answers2

0

I hope you had something like this in mind. A guessing game that takes two inputs. Maximum integer and the users number choice and outputs if the user is correct or not. Game doesn't end because of the while-loop.


Working Code:

import random


while True:

    try:

        print("Welcome to GUESS THE NUMBER. Up to what number would you like to go to?")
        maxNum = int(input())

        if maxNum <= 1:
            print('Number has to be greater than 0')
            break            

        print("Guess the number:")
        choice = int(input())

        if choice <= 1:
            print('Your Choice has to be greater than 0')
            break

        correctNumber = random.randint(0,maxNum)

        if choice == correctNumber:
            print('CORRECT')
            break

        else:
            print('WRONG')

    except:
        ValueError
        print('Enter a number, not something else you idiot')

And here the same code but the user only has 3 retries: (The indentations can be wrong if you copy paste my code)


import random

retry = 3

while retry != 0:

    try:

        print("Welcome to GUESS THE NUMBER. Up to what number would you like to go to?")
        maxNum = int(input())

        if maxNum <= 1:
            print('Number has to be greater than 0')
            break            

        print("Guess the number:")
        choice = int(input())

        if choice <= 1:
            print('Your Choice has to be greater than 0')
            break

        correctNumber = random.randint(0,maxNum)

        if choice == correctNumber:
            print('CORRECT')
            break


        else:
            print('WRONG')
            retry -= 1
    
    except:
            ValueError
            print('Enter a number, not something else you idiot')
    
    
if retry == 0:
    print('You LOOSER')
Maximilian Freitag
  • 939
  • 2
  • 10
  • 26
0

importing python builtin module

import random

let's say

num = 10

random.randint(1,num)

the retuerned integer will be between 1, 10