Python Forum

Full Version: Simple question - ERROR
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi guys, i'm just learning how to program in Python and i'm have some problems...

I'm testing the functions and this is my code:

##I want a simple code that tells you to type name and age then the program will tell it again.
##But i'm having problems with the while loop. I want it to ask again if an int is not typed for the age, but
##this code apparently is not enough. I've tried a lot of things, with if and exceptions, but i just can't.
##Can you help me please?

def display(nome, idade):
print("Seu nome é", nome, "e sua idade é", idade) #Your name is: // Your age is:

age = None
name = input("Qual o seu nome?") #What's your name?
while type(age) != int:
age = input("Qual a sua idade?") #How old are you?
print("O valor digitado não é um número inteiro") #The typed value is not an integer
display(name, age)
The input is a type str. So you have to convert to type int if you want to. Or you can use isalnum or isdigit methods.

I
n [1]: age = '132'

In [2]: type(age)
Out[2]: str

In [3]: age.isalnum()
Out[3]: True

In [4]: age.isalnum??
Docstring:
S.isalnum() -> bool

Return True if all characters in S are alphanumeric
and there is at least one character in S, False otherwise.
Type:      builtin_function_or_method

In [5]: age.isdigit()
Out[5]: True

In [6]: age.isdigit??
Docstring:
S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
Type:      builtin_function_or_method

In [7]: