Welcome to Admin Junkies, Guest — join our community!

Register or log in to explore all our content and services for free on Admin Junkies.

Making Input Statements

Making input statements is vitally important because, of course, we might someone on the outside to give information, you know, like in a survey or whatever. Anyway, input statements aren't much in Python and probably not much in other languages either.

Getting Our Feet Wet

Let's dive into this topic with some examples. Notice that aside from strings, you need to declare the datatype so things don't mess up. I was wondering, though, as I'm not an expert, only a learner, can you also declare the str type?

Code:
your_gpa = float(input("What is your gpa?"))

print("Your gpa was recorded as" + " " + str(your_gpa) + ".")

your_age = int(input("What is your age?"))

print("Your age is recorded as" + " " + str(your_age) + ".")

your_school = input("What is your school?")

# Notice we didn't have to declare the str type.

print("Your school is recorded as" + " " +  (your_school)  + ".")

do_you_play_sports = bool(input("Do you play sports, true or false?"))

print("It is recorded that it is" + " " + str(do_you_play_sports) + " " + "that you play sports." + ".")

This is quite straightforward. Any questions? Have you run into Python applications where this stuff was probably used? How did it go?
 
Last edited:
Advertisement Placeholder
OK, so there's a few things I need to clarify here.

Everything in Python has a type, by definition, by design. Nothing exists in a typeless form - it literally can't. Things like bool(), int() and so on aren't 'declaring the type', they're *changing* it.

int('5') is 'take a string that is the character 5 and turn it into a whole number'
int('500') is 'take a string of 3 characters, a 5, a 0 and a 0 and turn it into a whole number'

input() as a function *always* returns a string - this is why you 'didn't need to declare it as a str' because it already is one. You do, however, need to do it for some of the other things - because you can't just 'add a number to a string' or somesuch. ("adding two strings" is really the process of sticking one on the end of the other, better known as concatenation, and is why you have to have str() around things that aren't strings to join them together)

One final note, bool(input(...)) doesn't do what you think it will do. Coercing to bool means 'is the value of the thing truthy or falsy' and reducing it down to True or False. Truthy values are numbers that aren't zero, values that aren't None, or lists/sets/dicts that aren't empty. Crucially in this case, a truthy string is one that contains content. So if you ask for 'true or false' in input() and get in 'false', bool('false') will return True because it *doesn't look at the value in that case*. It simply asks 'is this an empty string, yes/no, not empty therefore True'
 

Log in or register to unlock full forum benefits!

Log in or register to unlock full forum benefits!

Register

Register on Admin Junkies completely free.

Register now
Log in

If you have an account, please log in

Log in
Who read this thread (Total readers: 0)
No registered users viewing this thread.

Would You Rather #9

  • Start a forum in a popular but highly competitive niche

    Votes: 9 27.3%
  • Initiate a forum within a limited-known niche with zero competition

    Votes: 24 72.7%
Win this space by entering the Website of The Month Contest

Theme editor

Theme customizations

Graphic Backgrounds

Granite Backgrounds