1

I am a beginner and have started learning python.I am supposed to make a program in which the user has to guess a random number between 1 and 100. If the guess is not correct then it's fine else I'm supposed to display message such as :"You're guess is far from the number " or if the guess is within 10 units of range of the number , I should display message such as You're 10 units in range of the number.

Now I'm dealing with a kind of logical error here .To explain this,if the random number is 42 and the user enters 32,the message displays :"You're 10 units in range of the number." which is fine but when I enter 32 ...It displays again:"You're 10 units in range of the number."

What should i do in the if-else to fix the code.I need to display if user is in range of 10 units to the umber to be guessed else I must print you're far from the guess.

from random import *
from time import *

number= randint(1,100)
print(number)
start=time()

guess=0
while guess!=number:
    guess=int(input())
    if number-guess <=10 :
        print("you are in range of 10 units to the number to be guessed.")
    else:
        print("You are very far in guess from that number.")


elapsed=time()-start
print("Time taken to guess the number correctly is ",elapsed)
6
  • 2
    I would write abs(number - guess). Else 20 - 80 = -60 <= 10. Commented Mar 12, 2017 at 11:39
  • Simply "run" your code mentally. You will quickly see what is wrong. Commented Mar 12, 2017 at 11:39
  • 1
    Add printouts of the variables inside the loop to see what happens to them. Commented Mar 12, 2017 at 11:40
  • Another suggestion. Do not use from random import * but write import random or from random import randint. Commented Mar 12, 2017 at 11:40
  • use abs(number - guess) so the distance is always positive Commented Mar 12, 2017 at 11:40

2 Answers 2

2

I checked your code there is nothing wrong and it's very nice but the only thing causing the error is that you have to "import math"before you write "time-start" your code should be like:

from random import *
from time import *
import math
number= randint(1,100)
print(number)
start=time()

guess=0
while guess!=number:
    guess=int(input())
    if abs(number-guess) <=10 :
        print("you are in range of 10 units to the number to be guessed.")
    else:
        print("You are very far in guess from that number.")


elapsed=time()-start
print("Time taken to guess the number correctly is ",elapsed)

***I was talking about the error at line 17 .the number problem will go away by using "abs()" just as the guy before me explained***.

Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain why this would make any difference?
if you run his code in IDLE there is the problem about the numbers that the guy before me solved the problem and suggested to use "abs()" but next to that just right before the program ends there will be an error referring to line 17 and the error is because of "time()-start"and when you use "import math" the error goes away.
OK. It would be good to clarify/explain this, since your answer is confusing if don't read the other answer; it should stand alone or refer explicitly to the other answer.
1

As Elmex80s write, you must change number-guess <=10 to abs(number - guess) <=10, because: Generated number is 20 and you enter 120 now you have -100 which is < 10. So final code to make this working normally:

from random import *
from time import *

number= randint(1,100)
print(number)
start=time()

guess=0
while guess!=number:
    guess=int(input())
    if abs(number - guess) <=10 :
        print("you are in range of 10 units to the number to be guessed.")
    else:
        print("You are very far in guess from that number.")


elapsed=time()-start
print("Time taken to guess the number correctly is ",elapsed)

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.