I'm learning Python, and decided to dab with a 'What year is it' thing. Here's what I have:
from datetime import datetime
now = datetime.now()
currentyear = now.year
userinput = input("What year is it? ")
if userinput == currentyear:
print ("Correct! The year is %s") % (currentyear)
However, it never prints out. What am I doing wrong?
As others have pointed out, Python 3 does not implicitly evaluate input. Using a call to int() will fix the issue.
from datetime import datetime
now = datetime.now()
currentyear = now.year
userinput = input("What year is it? ")
if int(userinput) == currentyear
print ("Correct! The year is %s") % (currentyear)
Related
# code 1
import time
while True:
from datetime import datetime
print(" Time: "+ "%d:%d:%d " % (datetime.now().hour,datetime.now().minute,datetime.now().second),
end = "\r")
time.sleep(1)
# code 2
name = input("Your good name please: ")
print("Hi "+name)
print('''Lets Play Guessing number game
''')
import random
import math
print("you have only 1 chance to guess the number")
original_no = random.randint(0,10)
player_input = input("Guess a number from 1 to 10: ")
if str(player_input) == original_no:
print("you have entered the correct number!")
elif str(player_input) != original_no:
print("you have entered wrong number")
print(str("the correct number is ") + str(original_no))
when the code gets executed, it only prints the code 1 and doesn't print the code 2.
I want to display time over the number guessing game.
I'm just a beginner ,please help me resolve this issue.
You need to end the while loop, you can do it by simply including a break statement in the loop or just remove while loop in case you want the time to be displayed only once.
while True:
from datetime import datetime
print(" Time: "+ "%d:%d:%d " % (datetime.now().hour,datetime.now().minute,datetime.now().second),
end = "\r")
break
time.sleep(1)
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.
I need to find the len() of multiple strings.
current_year = input ("What year is it?")
current_year = int(current_year)
birth_year = input ("What year were you born in?")
birth_year = str(birth_year)
if len(birth_year) != 4:
print ("Please make your year of birth 4 digits long.")
else:
birth_year = int(birth_year)
print("You are " + str(current_year - birth_year) + " years old.")
I would like to include the len() of birth_year.
Use an elif statement to check the current year input.
current_year = input ("What year is it?")
birth_year = input ("What year were you born in?")
if len(birth_year) != 4:
print ("Please make your year of birth 4 digits long.")
elif len(current_year) != 4:
print ("Please make the current year 4 digits long.")
else:
birth_year = int(birth_year)
current_year = int(current_year)
print("You are " + str(current_year - birth_year) + " years old.")
There's no need for birth_year = str(birth_year), since input() always returns a string.
You should probably include try/except code around the calls to int(), so you can print an error if they enter a year that isn't actually a number.
Here is a way to get what you want that is somewhat more dynamic. With this basic function, if a user inputs an inappropriate string, they will be asked to retry. You could even add a line to check if the year was after the current year.
Note that there is a loop in here with an exit that doesn't have a constraint. If you were going to implement this, I'd add a counter as well which would kill the process to prevent infinite loops.
def get_year(input_string):
# Set loop control Variable
err = True
# while loop control variable = true
while err == True:
# get input
input_year = input(input_string + ' ')
# Ensure format is correct
try:
# Check length
if len(input_year) == 4:
# Check Data type
input_year = int(input_year)
# if everything works, exit loop by changing variable
err = False
# if there was an error, re-enter the loop
except:
pass
# return integer version of input for math
return input_year
# set variables = function output
birth_year = get_year('What year were you born? Please use a YYYY format!\n')
current_year = get_year('What is the current year? Please use a YYYY format!\n')
# print output displaying age
print("You are " + str(current_year - birth_year) + " years old.")
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.
Ok, I'm gonna explain this as best I can.
I am trying to make an if/else statement in python that basically informs the user if they put in the right age based on raw input, but I keep getting an error in terms of my if statement. Can I get some help?
from datetime import datetime
now = datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"
name = raw_input("What is your name?")
age = raw_input("How old are you?")
if age == 1 and <= 100
print "Ah, so your name is %s, and you're %s years old. " % (name, age)
else:
print " Yeah right!"
change your if statement to this:
if age >=1 and age <=100:
two things:
if age >=1 and <=100 is missing age <=100
you are missing a : at the end
Here is the formatted and corrected code:
import datetime
now = datetime.datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"
name = raw_input("What is your name?")
age = raw_input("How old are you?")
while int(age) < 1 or int(age) > 100:
print " Yeah right!"
age = raw_input("How old are you?")
print "Ah, so your name is %s, and you're %s years old. " % (name, age)
Also, double check indents and your logic for the age.