© 2019 BrainStemSchool.com

December 13, 2016

December 9, 2016

December 7, 2016

Please reload

Input - non-string types

December 13, 2016

 

https://youtu.be/7Bwtj-NwOTE

Casting input to get what you want:

If you want numeric data from a string, you must assume that the user gave you what you want:

 

age=input(“What is your age?”)

print(“In 5 years you will be %i years old.” % int(age)+5 )

 

If you want (bad idea) you could cast the whole input line:

 

age=int(input(“What is your age?”))

print(“In 5 years you will be %i years old.” % age+5 )

 

You could also to the cast in its own line:

 

age=input(“What is your age?”)

age=int(age)

print(“In 5 years you will be %i years old.” % age+5 )

 

What if users don’t do what I want them to do?

 

What if they enter a float when I want an int?

What if they enter text or a combination of text and numbers when I just want numbers?

It can get tricky to deal with. We’re not going to worry about that in this introduction.

 

 

 

Share on Facebook
Share on Twitter
Please reload

Please reload

Recent Posts
Tags