Python Forum
Newbie to Python - input and branching issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie to Python - input and branching issue
#1
Big Grin 
Hi,

I'm newbie to Python.

I have a question on this coding, why I cant run this code?
Can anyone guide me what's problem to this.

Thanks

age = input ("How old are you? ")
height = input ("What is your height? (cm)")
if (age > 8) and (height > 130):
    print ("Welcome to our Roller Coaster!")
else:
    print ("Come again next time!")
Output:
How old are you? 9 What is your height? (cm)140
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-46-0851fb80a35f> in <module> 1 age = input ("How old are you? ") 2 height = input ("What is your height? (cm)") ----> 3 if (age > 8) and (height > 130): 4 print ("Welcome to our Roller Coaster!") 5 else: TypeError: '>' not supported between instances of 'str' and 'int'
Reply
#2
input returns a string, not an int. So, as the error says, it doesn't make sense to compare a string and a number. Use the int function to convert the value you get from input to an int.
Reply
#3
(Jul-30-2021, 06:27 AM)ndc85430 Wrote: input returns a string, not an int. So, as the error says, it doesn't make sense to compare a string and a number. Use the int function to convert the value you get from input to an int.

Thank you for your guide.
I got the solution.
YEAH! Smile
thanks so much

age = input ("How old are you? ")
height = input ("What is your height? (cm)")
if (int(age) >= 8) and (int(height) >= 150):
    print ("Welcome to our Roller Coaster!")
else:
    print ("Come again next time!")
Output:
How old are you? 9 What is your height? (cm)155 Welcome to our Roller Coaster!
ndc85430 likes this post
Reply
#4
Python is cool. It can do many things in 1 line.

split() is a built in function to split strings and return a list. By default it splits a string on a space character .

So, if

mystring = 'Hi my name is Tom.'

mystring.split() returns a list: ['Hi', 'my', 'name', 'is', 'Tom.']

If you wanted to split on say, m, just write

mystring.split('m')

This baby is called a list comprehension, a quick way of making a list:

mylist = [x for x in range(1, 10)]

You can combine list comprehension, input and split, take 2 (or more) variables which are numbers and do everything in 1 line:

From my Idle shell:

Quote:>>> var1, var2 = [int(x) for x in input("Enter your age in years and your height in centimetres here, separated by a space: ").split()]
Enter your age in years and your height in centimetres here, separated by a space: 8 130
>>> var1
8
>>> var2
130
>>>
Sherine likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python newbie laleebee 2 1,346 May-24-2022, 01:39 PM
Last Post: laleebee
  Experiencing a bug probably related to input() issue NewtoPython2020 1 1,950 Dec-05-2020, 08:13 AM
Last Post: perfringo
  Newbie on Python syntax rud0lp20 6 2,977 Apr-21-2020, 04:26 PM
Last Post: jefsummers
  python newbie marcush929 2 2,616 Jan-01-2020, 08:06 AM
Last Post: marcush929
  Newbie help with input LouK 9 4,330 Dec-22-2019, 04:12 AM
Last Post: onlydibs
Smile Help needed. Python Newbie!, it will be fun. knightdea 3 2,658 Oct-13-2019, 08:50 AM
Last Post: perfringo
  Python Newbie Environmental Meta Issue wallacebmann 2 2,424 Apr-26-2019, 02:08 PM
Last Post: Larz60+
  Branching and Conditionals/If statements MSL 1 1,959 Jan-26-2019, 11:52 PM
Last Post: Larz60+
  Python Linting (newbie) LieveHeer 2 2,644 Jan-24-2019, 05:36 PM
Last Post: LieveHeer
  Mixed string,Integer input variable issue maderdash 2 2,781 Nov-06-2018, 09:46 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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