dddictionary
dddictionary

Reputation: 75

how to add score and loop through game

import random
x = 50
score = 0

number = random.randint(1, x)

[print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
print('The largest possible number to guess is ' + str(x))

if number < x/2:
    print('This number is less than ' + str(int(x/2)))
else:
    print('This number is larger than ' + str(int(x/2)))

print(number)

while True:
    if int(input('Guess: ')) == number:
        print('You got it')
        break
    else:
        print('Try again!')

What the code does so far is takes a random integer between 1 and whatever number I want. It tells me which numbers it is divisible by between 1-9 and also if it is bigger than half the maximum possible number. It essentially gives you a lot of info to guess.

I want to add a score aspect where after you guess the correct number, you will get 1 added to your score. Then it will loop back to the beginning, get a new number to guess and give all it's information again so you can guess. I'm trying to get the looping part but I'm really lost right now.

Upvotes: 0

Views: 1474

Answers (2)

Alejandro Garcia
Alejandro Garcia

Reputation: 26

Haven't worked with Python myself before but I assume you want to encapsulate everything inside a huge while loop and ask at the end of each iteration if you want to keep playing

Something like this (this is more pseudocode than anything, didn't even tested it)

import random
x = 50
score = 0
keepPlaying = True
while keepPlaying:
    number = random.randint(1, x)
    [print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
    print('The largest possible number to guess is ' + str(x))

    if number < x/2:
        print('This number is less than ' + str(int(x/2)))
    else:
        print('This number is larger than ' + str(int(x/2)))

    print(number)

    if int(input('Guess: ')) == number:
        print('You got it')
        score++
        break
    else:
        print('Try again!')
        score--

    if (input("do want to keep playing?")=="no")
        keepPlaying = False

Upvotes: 0

Johan Jomy
Johan Jomy

Reputation: 691

When your guess is correct we can add 1 to your current score and print it. You can play till you guess it right. You have to put the whole code in a while loop for looping through the game after every correct answer. You can break the loop if your score is greater than 10 and the game stops.

import random
x = 50
score = 0

while True:
    if score >= 10:
        break

    number = random.randint(1, x)

    [print("This number is divisible by ", str(i)) for i in range(1, 10) if number % i == 0]
    print('The largest possible number to guess is ' + str(x))

    if number < x/2:
        print('This number is less than ' + str(int(x/2)))
    else:
        print('This number is larger than ' + str(int(x/2)))

    print(number)
    
    while True:
        if int(input('Guess: ')) == number:
            print('You got it')
            score+=1
            print('Your current score',score)
            break
        else:
            print('Try again!')

Upvotes: 1

Related Questions