Python Forum
Why isn't this code working?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why isn't this code working?
#1
I'm fairly new to python and just started yesterday. I gathered the most that I know and formed a little game or set of questions that the user would input his results and it'll come out. However, I reached a point where this one code would not work! Check it out, what have I done wrong.

number = input("type number")

if number is 5:
    print("correct")
else:
    print("wrong")
as soon as I input 5 in the system, it'll execute "wrong" even when the input is 5. Why?
Reply
#2
First, you want to use == (double equals) for comparison, not is. Second, input returns a string, not a number. So it returns '5', which is not the same as 5. So you either want to convert the input to an integer (number = int(number)), or you want to test against a string (if number == '5':).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-30-2017, 04:24 PM)ichabod801 Wrote: First, you want to use == (double equals) for comparison, not is. Second, input returns a string, not a number. So it returns '5', which is not the same as 5. So you either want to convert the input to an integer (number = int(number)), or you want to test against a string (if number == '5':).

I feel incredibly stupid! However, I used this line of code:
number = float(input("type number here"))

#instead of

number = int(number)

#I also tried this

number = int(input("type number here"))


'''
and it also worked. However, whats the difference between float and int? when do you use float or int?
or it doesn't matter?

'''
Reply
#4
myfloat = 1.23456
myint = int(float)
print('myfloat: {}, myint: {}'.format(myfloat, myint))
mynewfloat = float(myint)
print('mynewfloat: {}'.format(mynewfloat))
an integer is a whole number like 1, 2, 3 ,4 etc.
a float is a number like 1.0, 1.035, 76.4, etc. It contains a mantissa and an exponent.

you can convert a float to an integer, and you will lose any decimal portion
If you then convert back to a float, the decimal portion cannot be restored because it was lost.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 969 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 897 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,017 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,085 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,939 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,996 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,917 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,699 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 10,227 Dec-21-2020, 10:58 PM
Last Post: bowlofred
  autocomplete working code sample not working... aviper4u 0 1,645 Oct-24-2020, 03:04 AM
Last Post: aviper4u

Forum Jump:

User Panel Messages

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