Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple question - ERROR
#1
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)
Reply
#2
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]: 
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Question - ' defined as "a". ?' Ryan012 10 1,492 May-27-2023, 06:03 PM
Last Post: Ryan012
  Very simple question about filenames and backslashes! garynewport 4 1,832 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Python Tkinter Simple Multithreading Question AaronCatolico1 5 1,476 Dec-14-2022, 11:35 PM
Last Post: deanhystad
  A simple "If...Else" question from a beginner Serena2022 6 1,638 Jul-11-2022, 05:59 AM
Last Post: Serena2022
  Simple arithmetic question ebolisa 5 2,010 Dec-15-2021, 04:56 PM
Last Post: deanhystad
  Simple code question about lambda and tuples JasPyt 7 3,239 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  Simple question 1234 4 2,228 Dec-04-2020, 12:29 PM
Last Post: DeaD_EyE
  Simple Timer Question cranberrica 3 2,141 Jun-22-2020, 06:29 PM
Last Post: cranberrica
  Simple code error please help bntayfur 7 3,392 Jun-08-2020, 02:22 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020