Brainlet corner: Python edition

Basically I want to screen out non integers when asking for an age. How can I rewrite this where it converts the user input to a int?

age = input("State your age: ")

while not age.isdigit():
age = input("Numbers only: ")
print("Input OK...")

if age

Attached: file.png (601x203, 16K)

You can convert a string into an int by using int().
You know how I know this? Because I used documentation. Learn to read and search.

I tired that earlier but it didnt work... here is what I did:

age = input("State your age: ")

while not age.isdigit():
age = int(input("Numbers only: "))
print("Input OK...")

if age

Cant you just tell a google app what you want it to do and it will make the code for you??

If this is a total fuckup, how should it be rewritten? I understand the basic concepts in isolation, but when I try to combine them thats when it goes to shit.

Wow, you're an idiot.

Look trough your code one line at a time and try to identify the nests. Since you're slow I'm gonna say it to you. your int() is inside the while nest which only triggers if the user tried a wrong input. Worse than that, if you try to int() something that can't be int()'ed python iirc throws an exception. What I would do is spare the first line and put a more descriptive input function inside a while loop and once the while loops validate the input I would do age = int(age) on the next line outside the nest.

Attached: HorribleSubs_Darling_in_the_FranXX_22_720p.mkv_snapshot_00.45_2018.06.23_18.09.15.jpg (1280x720, 72K)

isdigit only checks if the string represents a number. It doesn't make it usable as a number. Once you turn your input into a number with int() it stop being a string.
You should do age = int(age) after it passes your test.

when you use int() it's going to convert eg floats anyway.
what you want is literal_eval

Can you show me so I can compare? I know if I miss a dent or a colon the fucker wont run and I wont know if the overall concept is wrong or just a syntactical thing

i.e. please write my shit program so I can see how it should be done

That still isn't correct buddy, refer to my version below:
age = input("State your age: ")

while not age.isdigit():
age = input("Numbers only: ")
print("Input OK...")

age = int(age)

if age

Fire up a debugger or, if you have no other option, throw in print()s. See what the values are, where they aren't what they should be, then what made them not be right. RTFM where needed.

If you want to learn shit, figure it out on your own. If you want to learn how to learn shit, ask. If you want it *done*, find a freelancer.

I'm on my phone, moron. But okay, sure.

age = ""
while not age.isdigit():
age = input("State your age (Numbers only): "
age = int(age)
message = "You pay: "
if age < 5:
message.join("Free entry.")
elif age < 8:
message.join("Five.")
else:
message.join("Ten.")
print(message)

Attached: 1529566992315.jpg (409x410, 36K)

i don't think that runs correctly at all.

age = input("State your age: ")

while not type(age) is int:
age = input("numbah onry:")

print("Input OK...")
age = int(age)
print("You pay:%s" % ("Free entry." if age

Pay more attention to types op, I think that's what mixed you up. int() turns a string to an integer. Only strings have a method called isdigit (). Strings can't have /= used on them.

I had the same fix in mind.
Python's documentation is shit, especially for newbies.
It does

fails on 17 for example

This was my code, and I ran it before I posted...

It runs correctly on python3 and not python2 because python2 auto converts to int. isdigit checks if a string is comprised only of digits, not if it's a single digit.

Your code does indeed work, thank you for your help.

Can anyone offer an explanation as to why it works and why it (if it is) a sub optimal way of doing things?

>Jow Forums is this bad
why wouldn't you just write the condition as age%1

It works because I did an int conversion on the string named "age", therefore enabling you to do a comparison with 4 and 8. Otherwise, it'll error and tell you that you can't compare a string with an int

>not using the dot operator

There's nothing really suboptimal about it. Anything left is incredibly trivial nitpicking about moving constructs around and type checking differently.
It works where the other iterations didn't because it avoids trying to call isdigit() on an integer AND avoids trying to call

age = float(input("State your age: "))

while age%1:
try:
age = float(input("Numbers only: "))
except ValueError:
continue
print("Input OK...")

if age

you might be more mixed up than OP is

works fine on all versions of python you dumb nigger

it breaks if you enter a non-number on all versions and I have no idea what you think that %1 is doing.

Yeah okay it runs but you've used float whereas we're dealing with age which is int, other than that, this is another valid response to OP's problem

It's not a valid response to OP's problem.

I thought the point was to not allow fractional ages

count = -1
age = .5
while age%1:
try:
count +=1
age = float(input("Numbers only: " if count else "State your age:"))

except:
continue
print("Input OK...")

if age

unless you make something that works on both versions it is

ahaha what the fuck is this meme tier code

make calling isdigit work on 2.7 :)

where are you learning python from?

while x.isdigit() == False

State your age: big dicks
File "", line 1
big dicks
^
SyntaxError: unexpected EOF while parsing

:

obviously i put that in.
and the two statements are equivalent in 2 and 3 anyway.

def getAge(arg):
try:
return int(input(arg))
except:
return "invalid input"

age = getAge("State your Age: ")
while not isinstance(age, int):
age = getAge("Numbers Only: ")
print("Input OK...")

if age

int(input())

342.432
Out[8]: 342

>input()
I always forget this is a thing. I don't think I've ever used it, even when I was first learning Python.

wut
Are you stuck at python 2

No. How does that even related? Python 2 has input() just like Python 3 does.

With python2 it's more believable as you'd probably use raw_input instead
But don't tell me you never used the standard stdin function even when first learning

age = list(input("Age?"))
result = list(filter(lambda i : i.isdigit(), age))
print(join('' , result)


Quit what you're doing and go to Codewards OP

Never used it, never saw the point. If my script needed user input I've always just hard coded the value or parsed it from sys.argv().

I never bothered with any of this "Hello World: Python edition" crap though. Some MATLAB courses in college taught me the basics of programming and everything else I learned from jumping straight into projects.

Jow Forums, apparently.

Turn join into str.join

I did a type on the last line. Should be
print(str.join('' , result))