The handling of numbers in Python

Related searches

guys. I'm new to Python and I got a simple question about the handling of numbers in this language that I couldn't figure out. It's a word guessing game that I was trying to build.

answer = "SOPHIA"
guess = ""
attempt = 3


guess = input("TYPE YOUR ANSWER: ")

while guess != answer and attempt > 1 :
    attempt = attempt - 1
    print("WRONG ANSWER. ATTEMPT REMAINING: " + str(attempt))
    guess = input("TYPE YOUR ANSWER AGAIN: ")

if attempt == 0:
    print("NO MORE ATTEMPT REMAINING.")
else:
    print("CORRECT ANSWER.")

And when I executed the above code, the outcome was

TYPE YOUR ANSWER: Leon
WRONG ANSWER. ATTEMPT REMAINING: 2
TYPE YOUR ANSWER AGAIN: Raphael
WRONG ANSWER. ATTEMPT REMAINING: 1
TYPE YOUR ANSWER AGAIN: Michael
CORRECT ANSWER.

I tried to include the numbers in int() but got the same result.

After giving a wrong answer three times, I should have got "NO MORE ATTEMPT REMAINING." while I got "CORRECT ANSWER.". Did I misunderstand the working of numbers in Python. I know the answer should be simple but I still can't get it. Could anyone tell me where I got wrong?

Thank you, guys.

You should change your loop condition to this:

while guess != answer and attempt > 0:

In your previous version

while guess != answer and attempt > 1:

You stop loop when attempt=1 and that's why this condition if attempt == 0: never true.

Python Numbers, Type Conversion and Mathematics, Python supports a "bignum" integer type which can work with arbitrarily large numbers. In Python 2.5+, this type is called long and is separate� Yes; Python 2.x has two types of integers, int of limited size and long of unlimited size.However all calculations will automatically convert to long if needed. Handling big numbers works fine, but one of the slower things will be if you try to print the 100000 digits to output, or even try to create a string from it.

Use this :

while guess != answer and attempt >= 1

Actually your code, in case of wrong answer third time, isn't changing attempt variable to 0 and attempt variable remains 1 so else block is being executed

Python - Numbers, Stop at any finite number of bits, and you get an approximation. On most machines today, floats are approximated using a binary fraction with the numerator� Handling very very small numbers in Python. Ask Question Asked 8 months ago. Active 8 months ago. Viewed 534 times 7. I need to multiply about 1e6 numbers

I am also new but i think it is the logic which has issue. Can u try with while guess != answer and attempt > 0 :

Python Numbers, Python File Handling Python Read Files Python Write/Create Files Python Delete Files Learn how to add two numbers in Python. Use the + operator to add two numbers

TYPE YOUR ANSWER: ken
WRONG ANSWER. ATTEMPT REMAINING: 2
TYPE YOUR ANSWER AGAIN: seth
WRONG ANSWER. ATTEMPT REMAINING: 1
TYPE YOUR ANSWER AGAIN: cue
1
CORRECT ANSWER.

I ran your program with a simple print statement above your conditional and this is what I got. My guess is that your looping conditional is wrong. Here is what I would do:

answer = "SOPHIA"
guess = ""
attempt = 3

while attempt > 0 :
    guess = input("TYPE YOUR ANSWER: ")
    if guess == answer:
        break
    attempt = attempt - 1
    print("WRONG ANSWER. ATTEMPT REMAINING: ")


if attempt == 0:
    print("NO MORE ATTEMPT REMAINING.")
else:
    print("CORRECT ANSWER.")

That way, you get the number of iterations you want. Otherwise, if you just change the conditional to:

while guess != answer and attempt > 0:

Then you get this edge case:

TYPE YOUR ANSWER: ken
WRONG ANSWER. ATTEMPT REMAINING: 
TYPE YOUR ANSWER: brian
WRONG ANSWER. ATTEMPT REMAINING: 
TYPE YOUR ANSWER: abdul
WRONG ANSWER. ATTEMPT REMAINING:
TYPE YOUR ANSWER: SOPHIA
WRONG ANSWER. ATTEMPT REMAINING:
NO MORE ATTEMPT REMAINING.

Handling very large numbers in Python, Python has a built-in round () function that takes two numeric arguments, n and ndigits, and returns the number n rounded to ndigits. The ndigits argument defaults to zero, so leaving it out results in a number rounded to an integer. As you’ll see, round () may not work quite as you expect.

15. Floating Point Arithmetic: Issues and Limitations — Python 3.8.5 , NumPy does have support for masked arrays – that is, arrays that have a separate Boolean mask array attached for marking data as "good" or "bad." Pandas could have derived from this, but the overhead in both storage, computation, and code maintenance makes that an unattractive choice.

Comments
  • You might want to check what value attempt has when you exit the while loop.
  • Your while loop is exited after 2 loops, with attempt == 1
  • This has nothing to do with Python and everything to do with logic. Think about what happens if attempt is equal to 1 and you compare attempt > 1.