Problems with this python code

import random as ra
age=input( " how old are you  ")
name = input(" what is your name")
print( "Your name is " +name, "you are " +age, "years old")

for i in range (0, 30):
  a = ra.randrange(100) 
  print(a)
  b = ra.randrange(100) 
  c = a + b
  print( " what is the sum of " , a, 'and',b)
  number = int(input( "please enter the sum"))
  if number==c:
    print(" You are correct "+name)
    break
  else:
    print( " Please try again "+name)

I’ve edited the original post to make it more readable. Specifically, I added three backticks before the code block and three backticks after.

You can post code in two ways. Single word/line snippets can be handled inline. Multiple lines should be handled in code blocks:

Inline

Wrap your code in a single back ticks:

Example:

Code Blocks

Wrap your code in triple back ticks, with new lines between the back ticks and code:

Example:

Typing Backticks:arrow_heading_up:

QWERTY and QWERTZ (Key that’s been marked with red border)

Demo courtesy of @JacksonBates

Hi @jindal25
The code seems to work pretty fine without any error.
Can you please be more specific about what problems you are facing with the python code?

The code works but I assume you would like to try again and again until the result is OK then break:

In that case I propose to try:

while number != c:
      print( " Please try again "+name)
      number = int(input( "please enter the sum"))
else:
      print(" You are correct "+name)
      break

after your:

number = int(input( "please enter the sum"))

thanks guys … it helped a lot.

Are there any good tutorials which help in learning python with lots of examples ?

rgds,
Pankaj

have you tried the freecodecamp curriculum?

1 Like