Python Forum
Repeat question (for loop)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repeat question (for loop)
#1
Trying to make a small program where a user can answer basic questions (1+1 etc). But the questions should be random, in my code, no random numbers are generated. Is the for loop in wrong place?

import random

from random import randrange



x = random.randrange(0, 101)

y = random.randrange(0, 101)

sum = (x + y)

for sum in range(1, 5):

	a = int(input(f'What is {x} + {y}? '))

if a == sum:
    	print('Correct')

else:
    	print(f'Not correct, the correct answer is {sum}.')
Reply
#2
There are a few issues. To begin with, sum is a function name in python, so it's not a good idea to use that as a variable name. Also, you declare that variable in line 11 but then you overwrite it in line 13 by using it as your variable in the for loop. Your random number generation AND your if/else should take place inside your for loop. With the current code, you are looping over the same question 5 times before you get to the if statement.

Here is the general structure you need:
# Imports

# For loop starts
    # Generate random numbers and variable for total
    # Ask question and get input
    # If/else to check answer
Reply
#3
  • sum is built-in function and you should not use it as variable name
  • on line 13 you overwrite its value (i.e. it is no longer the correct result of x+y
  • lines 17-21 should be inside the loop - i.e. you want to check the answer of the user every time you ask
  • it's not clear why you want to ask 5 times the question - is it that you want to ask 5 different questions or user has 5 tries to answer correctly? Depending on the answer of this question you may need to move lines 7-11 inside the loop or if the later - to break out of the loop in case of correct answer
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jun-17-2020, 12:44 PM)buran Wrote:
  • sum is built-in function and you should not use it as variable name
    ...is it that you want to ask 5 different questions

Oh, will never use sum as a variable again. :) I was unclear, should be 5 different questions.
Reply
#5
Updated code, still no success. User is beeing asked only once. So I am thinking of the for loop which contains the variable i. Why? Well, good question. I am just learning by doing so would like to have some feedback.

import random

from random import randrange

for i in range(1, 5):	

	x = random.randrange(0, 101)

	y = random.randrange(0, 101)

	tot = (x * y)

a = int(input(f'What is {x} + {y}? '))

if a == tot:

	print('Correct')

else:
	print(f'Not correct, the correct answer is {tot}.')
Reply
#6
lines 13-20 are still outside the loop
tot is result of multiplication, but the question is about addition
with range(1, 5) you will ask 4 questions

import random
 
for i in range(1, 5):   # ask 4 questions. You can do just range(4)
    x = random.randrange(0, 101)
    y = random.randrange(0, 101)
    tot = x + y

    answer = int(input(f'What is {x} + {y}? '))
    if answer == tot:
        print('Correct')
    else:
        print(f'Not correct, the correct answer is {tot}.')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Look at the indentation of lines 13 to 20. If you want that part to loop it needs to be indented with the rest of the for loop above it. Also, check the docs on the range() function. Your loop will not iterate 5 times ut only 4.
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply
#8
Thanks for your feedback (and patience). Surely, will read more about range and specially indent.

import random
 
from random import randrange
 
for i in range(1, 6):   
 
	x = random.randrange(0, 101)
 
	y = random.randrange(0, 101)
 
	tot = (x + y)
 
	a = int(input(f'What is {x} + {y}? '))
 
	if a == tot:
 
		print('Correct')
 
	else:
		print(f'Not correct, the correct answer is {tot}.')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,741 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,481 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,729 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat keywords at end of every line knob 4 2,225 Sep-15-2020, 06:38 AM
Last Post: perfringo
  Question about running comparisons through loop from input value Sunioj 2 2,362 Oct-15-2019, 03:15 PM
Last Post: jefsummers
  How to repeat input line of code until condition is met Reta 2 3,331 May-14-2019, 10:06 PM
Last Post: nilamo
  while loop question Tripler 4 2,887 Jul-24-2018, 06:37 AM
Last Post: buran
  Loop question kraven 3 3,567 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 3,511 Jul-23-2017, 03:01 AM
Last Post: Pires
  Udacity while loop question liquidmetalrob 6 5,276 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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