Input - non-string types


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.

Recent Posts
Tags

© 2019 BrainStemSchool.com