Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program for random sum
#1
Hi,

I started learning Python yesterday. I'm trying to create a program that generates a sum of two random integers for the user to calculate. I want the code to generate a new calculation if the user chooses Yes after inputting the correct result and prompt the user to try again if the result is incorrect, but I can't figure out how to do it. Any hints, please? Thank you :)

This is what I have so far, but obviously wrong:

import random

def random_calculation():

    number1 = random.randint(1,100)
    number2 = random.randint(1,100)
    result = number1 + number2

    user_result = int(input(f"Calculate: {number1} + {number2} = "))

    if result == user_result:
        new_calculation = input("Correct! New calculation? (Y/N) ")
        if new_calculation == Y:
            random_calculation()
    else:
        while result != user_result:
            input(f"Incorrect. Try again. \nCalculate {number1} + {number2} = ")


random_calculation()
Reply


Messages In This Thread
Program for random sum - by Cristopher - Jan-15-2022, 03:04 PM
RE: Program for random sum - by deanhystad - Jan-15-2022, 03:31 PM
RE: Program for random sum - by Cristopher - Jan-15-2022, 03:38 PM
RE: Program for random sum - by Yoriz - Jan-15-2022, 03:48 PM
RE: Program for random sum - by Cristopher - Jan-15-2022, 06:35 PM
RE: Program for random sum - by Yoriz - Jan-15-2022, 06:50 PM
RE: Program for random sum - by deanhystad - Jan-15-2022, 07:12 PM
RE: Program for random sum - by Cristopher - Jan-15-2022, 07:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Noob here. Random quiz program. Using a while loop function and alot of conditionals. monkeydesu 6 1,491 Sep-07-2022, 02:01 AM
Last Post: kaega2
  Random Dice roll program th3h0bb5 1 5,588 Oct-18-2016, 09:25 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020