My python program is saying the answer is not correct when it is?

how to fix invalid syntax in python
python yes/no loop
how to code a yes or no question in python
syntax error python
in python, a syntax error is detected by the interpreter
runtime error python
online python compiler
invalid syntax python print

I do not know why this is saying it is wrong when it isn't. I enter the correct answer and it says that is not correct the correct answer is what i inputted

import random
x = 1
score = 0
num = 1
while x == 1:
    a = random.randint(10, 201)
    b = random.randint(1, 201)
    a = int(a)
    b = int(b)
    c = a / 100 * b
    print ("What is")
    print (b)
    print ("% of")
    print (a)
    num = input()
    if c is num :
        print ("Well done!")
        score = score + 1
    elif c != num :
        print ("That is not correct, the correct answer is ", c)

There is a difference between '==' and 'is' in python.

'==' checks if the value is the same, while 'is' checks if they are the exact same object See also: Is there a difference between `==` and `is` in Python?

Also, input() returns a string, but you compare it to a number. You have to convert the user input to a number first by using int(num) or float(num)

Intro Python Debugging, Syntax errors can be detected before your program begins to run. The best solution is to avoid this problem by running your program after you write every Lastly, the actual NameError says that global name 'hello' is not defined. Place your cursor within idle and move it until you are on the correct line as indicated by​  Python Command Line: In Windows if you open up a command line window, what used to be called a DOS box, and run python, the system will respond with the Python command prompt >>>. At this point you’re running Python in it’s interactive mode, just like when you’re inside of IDLE.

use of is comparison in this case is incorrect, is comparison is based on the object.

you should be using == as you need to compare actual values of the variables

so this

if c is num :

shouls be this

if (c == num):

Reactive Programming with JavaScript, C++ often throws you into the Pit of Despair and you have to climb your way up to Now as I've said before, the design of C# is not a subtractive process. language, a language whose rules encourage you to write correct code in the first how to find a particular kind of vulnerability is answered by saying that Python had  Summary – Python functions quiz part-1. We hope that you’d enjoyed going through the above Python functions quiz part-1. We host a no. of other quizzes on various programming topics in Python. You can follow the below section to find out the one that interests you the most.

import random
x = 1
score = 0
num = 1
while x == 1:
    a = random.randint(10, 201)
    b = random.randint(1, 201)
    c = a / 100 * (b *1.0) # adding ‘* 1.0’ makes sure c is a float. 
    print ("What is %s\% of %s?" %(b, a)) # combined the prints in one line
    num = float(input()) # Change input to float
    if c == num :
        print ("Well done!")
        score = score + 1
    elif c != num :
        print ("That is not correct, the correct answer is ", c)

Exploratory Programming for the Arts and Humanities, According to my original statement, “Consider an attempt to add five and five together The Python interpreter correctly accepts that “(5 + 5) * (3 - 1)” actually is an arithmetic to another, from giving one formally wrong answer to another formally wrong answer. (Strictly speaking, one could say that it isn't even a program. ''' Online Python Debugger. Code, Run and Debug Python program online. Write your code in this editor and press "Debug" button to debug program.

I'm new to Python, how can I write a yes/no question?, In its simplest form you just ask the question, get the answer and process the answer, i.e.: You can go further and loop the question to allow for repeated incorrect input: How long does it take to make a programming language like Python? In this slice of code, you will give your app the ability to detect any word starting  Previous: Write a Python program to calculate a grid of hexagon coordinates of the given radius given lower-left and upper-right coordinates. The function will return a list of lists containing 6 tuples of x, y point coordinates.

The 10 Most Common Mistakes That Python Developers Make, Test your Python knowledge by seeing how many of these 10 most common The more advanced Python programming answer is that the default value for a So in the above code, since the attribute x is not found in class C , it will be Let's say you have two files, a.py and b.py , each of which imports the other, as follows:. The chained assignment above assigns 300 to the variables a, b, and c simultaneously.. Variable Types in Python. In many programming languages, variables are statically typed. That means a variable is initially declared to have a specific data type, and any value assigned to it during its lifetime must always have that typ

Sage for Undergraduates, The output produced below illustrates the point: You get a wrong answer if you of $ 0.610000000000000 Unlike most computer languages, Python has a solution following response, indicating that it understood what we were hoping to say, In SageMathCloud, below your code working with coinCount, type on a line  The issue with your code segment is that my_dict is declared inside the word_count method and then your trying to access it outside the method, where it doesn't have scope. def word_count(b): word_list = b.split() ret_val = {} for word in word_list: if word in ret_val: ret_val[word] += 1 else: ret_val[word] = 1 return ret_val my_dict = word

Comments
  • Don't use is for such comparison, use ==. Additionally c is a float due to the division so it may be slightly different from the exact integer value.
  • Thank you so much have been fiddling with this for hours and it was such a simple fix. :-)