Reputation: 11
The task wants you to have the user enter a month, day and two-digit number for a year in sperate prompts and if the month times the day equals the year to print "this date is magic" but I'm getting an unknown error
What to be expected is: Sample Run (user input shown in bold) Enter month (numeric):12↵ Enter day:8↵ Enter two digit year:96↵ This date is magic! Sample Run (user input shown in bold) Enter month (numeric):10↵ Enter day:2↵ Enter two-digit year:75↵ This date is not magic
print('Enter a month:')
month=input('Select a month from the year')
print('Enter day:')
day=input('Select a day:')
print('Enter a two digit year:')
year=input('Select a two digit value')
if month*day=year:
print('This date is magic')
else:
print("This date is not magic")
``
Upvotes: 0
Views: 144
Reputation: 3113
The result of any input()
in python is a string. So then you are trying to multiply the string '4'
by the string '8
', for example.
Set everything to integers with int()
.
print('Enter a month:')
month=int(input('Select a month from the year'))
print('Enter day:')
day=int(input('Select a day:'))
print('Enter a two digit year:')
year=int(input('Select a two digit value'))
if month*day==year: # you also need == to do a comparison. single = sets a variable
print('This date is magic')
else:
print("This date is not magic")
Testing this with bad data entry you'll notice it crashes (if someone enters 'June' for the month, then int('June')
will give an error), so consider your data validation as well, but that's another step.
Upvotes: 1
Reputation:
You need to cast your inputs into ints.
Also you need to use the comparison operator ==
instead of the assignation operator =
inside your if.
print('Enter a month:')
month = int(input('Select a month from the year'))
print('Enter day:')
day = int(input('Select a day:'))
print('Enter a two digit year:')
year = int(input('Select a two digit value'))
if month * day == year:
print('This date is magic')
else:
print("This date is not magic")
Upvotes: 1