简体   繁体   中英

Python guessing 2 numbers game

Question is

Write a program that prints to the user the sum (a+b) and product (a*b) of two randomly generated natural numbers (positive integers) between 1 and 100. To win the game the user must guess the two numbers, the order of the guess should not matter. The user is given three guesses, if they are wrong after three guesses then the numbers are revealed to them. The user has to guess both guesses correctly in a single round and is not told if one of the numbers in a previous round was correct. The user has 3 attempts to guess the two numbers, and the game should finish if the user guesses the numbers correctly.

import random
a = random.randint(1,100)
b = random.randint(1,100)

print("Sum of two random number is",a + b)
print("Product of two random number is", a * b)
print("Try to guess the 2 numbers that make this sum and product, you have 3 tries, good luck!")

count = 0
win = 0

while count != 3:
    guess_a = int(input('Guess first number '))
    guess_b = int(input('Guess second number '))
    
    if count == 3:
        break
        
    if ((guess_a == a) or (guess_b == b)) and ((guess_b == a) or (guess_a == b)):
        win = 1
        break
        
    else:
        count = count + 1
        print('Incorrect!!!')

if win == 1:
    print('Congrats you won the game.')
    
else:
    print('You lost the game.')
    print('Number a was:',a)
    print('Number b was',b)

I tried this it kinda works, but I don't know how to make it that the order of the guessed numbers doesn't matter.

One way to do it is to not use booleans at all.

Since 0 is automatically converted to False in an conditional statement, we can just make an expression that equals 0 if a certain condition is true.

The expression I came up with is:

is_guess_a_right = (a - guess_a) * (a - guess_b)
is_guess_b_right = (b - guess_a) * (b - guess_b)
are_both_right = is_guess_a_right + is_guess_b_right

If the final expression is equal to zero, then both expressions are correct, in otherwords, guess_a and guess_b are equal to a and b (since a - guess_a would be equal to zero if they are the same).

Final code:

import random

a = random.randint(1, 100)
b = random.randint(1, 100)

print("Sum of a and b is %s" % (a + b))
print("Product of a and b is %s" % (a * b))
print("You have three tries to guess the numbers")

count = 0

while count < 3:
    guess_a = int(input("Guess the first number: "))
    guess_b = int(input("Guess the second number: "))

    lose = (guess_a - a) * (guess_a - b) + (guess_b - a) * (guess_b - b)

    if lose:
        print("Try again")
        count += 1
    else:
        print("Congrats, you won the game")
        print("The two numbers were %s and %s" % (a, b))
        break

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM