-1

I was told to rrite a program that asks for and reads in two dates, specified as integers for day and month (two variables for each date). Example 24 and 12 for December 24.

( Write an if-test that tests which date comes first in the same year:

  1. If the first date comes first, your program should print “Correct order!"
  2. If the last date entered is an earlier date, the program shall print “Wrong order!”
  3. If the dates are the same, write "Same date!" out.)

Code

month1 = input("choose a month by writing a number from 1-12!")
month2 = input("choose a month by writing a number from 1-12!")

date1= input("choose a date by writing a number from 1-30!")
date2 = input("choose a date by writing a number from 1-30!") 

if month1 > month2:
    print("right order! ")

elif month1 < month2:
    print("wrong order! ")


if date1 > date2:
    print("right order!")

if date1 < date2:
    print(" wrong order! ")

elif month1 == month2:
    print("same date! ")

    
elif date1 == date2:
    print("same date! ")
DarrylG
  • 16,732
  • 2
  • 17
  • 23
sena
  • 1
  • 1
    It's important to convert the values to numbers before the comparison. Currently, you are during a string comparison which will provide undesirable results (i.e. the output of function `input` is a string). See [How to read input as numbers](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – DarrylG Aug 27 '22 at 11:47

1 Answers1

1

Doing like this will generate multiple "Correct Order" and "Wrong Order" statements. and the requirement is to only get one response either "Correct Order" or "Wrong Order", for that all the if/else statement's must be in the same loop and this is how the code should look.

month1 = int(input("choose a month by writing a number from 1-12!"))
month2 = int(input("choose a month by writing a number from 1-12!"))

date1= int(input("choose a date by writing a number from 1-30!")) 
date2 = int(input("choose a date by writing a number from 1-30!"))

if month1>month2:
    print("Correct Order")
elif month1<month2:
    print("Wrong Order")
else: # Meaning both months are same, now we will compare the dates
    if date1 > date2:
        print("Correct Order")
    elif date1 < date2:
        print("Wrong Order")
    else:
        print("Same Date")
  • Not my downvote but two suggestions: 1) you need to convert values from strings to numbers and 2) although your conditional logic is correct, it would be simpler to continue with `elif` for date1 & date2 rather than starting an else. block with a nested `if-else`. – DarrylG Aug 27 '22 at 12:58
  • the int conversion was an oversight on my part when copying the original commands, I have fixed it. Else for the other if-else loop I think it's an easy way to make it clear how the program is working – ProjectRexa Aug 27 '22 at 15:58