Python Forum
storing input as a integer help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
storing input as a integer help
#1
import random
def function1():
a = random.randint(1,50)
b = random.randint(1,50)
c = a + b
print('what is',a,'plus',b,'?')
d = input()
if d > c:
print('your answer was incorrect, its a little too big')
if d < c:
print('your answer was incorrect, its a little higher')
else:
print('your answer was correct')
I just started coding and I dont understand whats wrong with this piece of code, whenever I try to execute the function, it says
'>' not supported between instances of 'str' and 'int'
but its a integer because I entered a number right? how do I fix this guys
Reply
#2
input() always returns a str. It doesn't matter what you type, it's a str.
>>> x = input("?")
?43
>>> x
'43'
>>> type(x)
<class 'str'>
If you want a number, then you'll need to convert it to one first:
>>> x = int(input("?"))
?42
>>> x
42
>>> type(x)
<class 'int'>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Verify input as integer and not blank GMCobraz 3 2,122 Jun-22-2020, 02:47 PM
Last Post: pyzyx3qwerty
  Mixed string,Integer input variable issue maderdash 2 2,745 Nov-06-2018, 09:46 AM
Last Post: snippsat
  Changing User Input Strings Into Set Integer Values Mario128843 3 3,150 May-01-2018, 03:32 AM
Last Post: Mario128843
  Storing float as integer in a database Ageir 6 5,234 Aug-17-2017, 08:03 PM
Last Post: Ageir
  why does integer input give error invalid literal for int() PyPhanman 2 6,178 Apr-27-2017, 05:32 AM
Last Post: ndc85430
  Taking user input and storing that to a variable then storing that variable to a list jowalk 12 37,317 Mar-27-2017, 11:45 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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