enter image description hereenter image description here
I tried everything I can It doesn't work pls help
print ("Enter today's date:")
DateToday = input()
print ("Enter your birthday {yyyy/mm/d)"
BDate = input()
Age = DateToday - BDate
print Age.year
I'm assuming you want just the year for age?
in which case you would do:
datetoday = int(input("Enter the current year: "))
bdate = int(input("Enter your birth year: "))
age = datetoday - bdate
print ("Your age is: " + str(age))
You forgot to add a few brackets and are asking for unnecessary data. This will give how many years old a person is.
Related
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 11 months ago.
I'm trying to use list in this one but I had the bug in the last line stumarks[CourseID] = [(StuID,marks)]
Here is my code :
courselist = []
class course:
def __init__(self,id,name):
self.id = id
self.name = name
#classmethod
def CourseInfo(cls):
return cls(
input("Enter the course ID : "),
input("Enter the course name : ")
)
def cif():
for i in range(getNumOfCourses()):
cse = course.CourseInfo()
courselist.append(cse)
stumarks = []
def MarkInput():
CourseID = input("Enter the course's ID : ")
if CourseID not in [CourseInfo.id for CourseInfo in courselist]:
print(" The course's id isn't founded! Please try again!")
else:
nm = int(input("Number of student that you want to enter marks: "))
for i in range(nm):
while True:
StuID = input("Enter a student's ID : ")
if StuID not in [StudentInfo.id for StudentInfo in studentlist]:
print("The student's ID isn't founded! Please try again! ")
continue
break
marks = float(input("Enter the mark: "))
if CourseID in stumarks:
stumarks[CourseID].append((StuID,marks))
else:
stumarks[CourseID] = [(StuID,marks)]
Could anyone help me with this one? Thank you!
The problem is that CourseId is not cast to an integer and is hence treated as a string. Try changing:
CourseID = input("Enter the course's ID : ")
to
CourseID = int(input("Enter the course's ID : "))
I don't know what is the error of my code can anyone help me to fix this code.
age = ""
while int(len(age)) == 0:
age = int(input("Enter your age: "))
print("Your age is " + age)
You wanted a string for age but you transformed this age into an integer inside your loop :)
You can write :
age = ""
while age == "":
age = input("Enter your age: ")
print("Your age is " + age)
In Python, "" and 0 (and values like None and empty containers) are considered "falsey". Not necessarily the same thing as False but logically treated like False.
So you can simplify your while loop:
age = ""
while not age:
age = input("Enter your age: ")
print("Your age is " + age)
You cast age to be an int inside of your while loop, while using the len(age) as the comparison.
int does not have a length.
You want to wait to cast age until you are outside of the loop, or in this situation, don't cast it at all.
age = ""
while len(age) == 0:
age = input("Enter your age: ")
print("Your age is " + age)
I have been set a task and it is the following, I was wondering would my code would work?
write a program to ask for a name and an age.
when both values have been entered, check if the person is the right
age to on an 18-30 holiday(they must be over 18 and under 31
name = input("What is your name? ")
age = int(input("How old are you ? "))
if 18 <= age < 31:
print("Hello",name,"welcome to this holiday.".format(name))
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")
well actually it works but you don't need the format part
name = input("What is your name? ")
age = int(input("How old are you ? "))
if age >=18 and age <31:
print("Hello",name,"welcome to this holiday.")
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")
if you want to use format you can do this
print("Hello {name} welcome to this holiday.".format(name=name))
Code works perfect, but you have a useless format
Just use format, not the , part.
name = input("What is your name? ")
age = int(input("How old are you ? "))
if 18 <= age < 31:
print("Hello {name} welcome to this holiday.".format(name))
else:
print("Sorry {name} you are not old enough to go on this holiday.".format(name))
Or if you don't like to do format
name = input("What is your name? ")
age = int(input("How old are you ? "))
if age >=18 and age <31:
print("Hello",name,"welcome to this holiday.")
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")
How can I let the last variable only accept number from 1 to 31 in a compact way?
stat = "none"
while stat != "Start" :
stat = input("Type\"Start\"to start generating: ")
if stat != "Start" :
print('Not started')
print(' ' ' Program started.Awnser the questions to generate a password ' ' ')
name = input("Enter your name: ")
name_last = input("Enter your last name: ",)
birth_year = input('Enter your birth year: ',int)
birth_day = input('Enter your birth day: ',int)
I don't know what programming language you are using but using python:
After you get the input,
birth_day = input('Enter your birth day (1-31) ')
you can check if it is valid and make them enter it again if it isn't:
while(int(birth_day)<1 or int(birth_day)>31):
print('Invalid value!')
birth_day = input('Enter your birth day (1-31) ')
Next time you ask a question, please specify the programming language and add it to the tags.
name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2014 - age)+100)
print(name + " will be 100 years old in the year " + year)
I am trying this code in python, but it is not executing, what corrections needs to be done?
From your question
name = input("What is your name: ")
age = int(input("How old are you: "))
year = str((2014 - age)+100)
print(name + " will be 100 years old in the year " + year)
Your code is just fine, change your input to raw_input for name variable.
NOTE: raw_input returns a string, input tries to run as Python expression.