Jump to content

Need help with my days of the month code in python

Johanes

i made this code when we had an activity where our teacher asked us to give the number of days in a given month.

 

import calendar

print("January=1, February=2, March=3, April=4, May=5, June=6,\nJuly=7, August=8, September=9, October=10, November=11, December=12\n")

userMonth=int(input("Which month are you looking for: "))

numberDays = (calendar.monthrange(2019,userMonth)[1])

print("This month has",numberDays,"days")

now i need help in making it so that the user can just either enter the name of the month or the number or even an abbreviation of the month(ex. January=jan, February=feb, etc.)

any tips or tricks to help me implement it?

Link to comment
Share on other sites

Link to post
Share on other sites

The easiest way is probably to make an 2D array of all the month names and the days each month takes. Then ask for user input and with this input, loop through the 2D array names (for loop) and see if the input matches any of the names (either exactly and you could probably implement a .Contains() like feature too. Not sure if Python has that).

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Since the first three letters of each month is unique, you can instruct the user to enter at least the first three letters of a month. Use find() to parse the input.

 

Then make a dictionary that associates each three character value with the number of the month.

Link to comment
Share on other sites

Link to post
Share on other sites

 

Get some string input from user, do a lowercase .then  you could look into an array for the strings. Fill the array with all possible combinations... it's not many ...less than 100.

 

 

you could make a function, something like this :

 

// you can write the base array in a nicer way
// value , string, minChars
$baseArray = [ "1:january:2","2:february:1", "3:march:3", "4:april:2", "5:may:3", "6:june:3", "7:july:3", "8:august:2", "9:september:1", "10:october:1", "11:november:1", "12:december:1"];

$keywords = array();
foreach ($baseArray as $value) {
	$parts = explode(":",$value); 
	$nr = intval($parts[0]);
	$text = $parts[1];
	$minLength = intval($parts[2]); 

	$length = strlen($parts[1]);
	for ($i=$minLength;$i<=$length;$i++) {
        $keywords[substr($text,0,$i)] = $nr;
        // k['ja']=1; k['jan']=1; ... k['january']=1;
    }
}

 

it's in php, but should be clear enough that you should be able to convert it to python or whatever.

Edited by mariushm
4 am here... forgot one month in the array
Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Johanes said:

i made this code when we had an activity where our teacher asked us to give the number of days in a given month.

 


import calendar

print("January=1, February=2, March=3, April=4, May=5, June=6,\nJuly=7, August=8, September=9, October=10, November=11, December=12\n")

userMonth=int(input("Which month are you looking for: "))

numberDays = (calendar.monthrange(2019,userMonth)[1])

print("This month has",numberDays,"days")

now i need help in making it so that the user can just either enter the name of the month or the number or even an abbreviation of the month(ex. January=jan, February=feb, etc.)

any tips or tricks to help me implement it?

what you need to do is create a map to map :P  user input to outputs

 

 

from calendar import monthrange
inputMap = {
  "1": monthrange(2019, 1)[1],
  "jan": monthrange(2019, 1)[1],
  "january": monthrange(2019, 1)[1],
  "2": monthrange(2019, 2)[1],
  "3": monthrange(2019, 3)[1],
  "4": monthrange(2019, 4)[1],
  "5": monthrange(2019, 5)[1],
  "6": monthrange(2019, 6)[1],
  "7": monthrange(2019, 7)[1],
  "8": monthrange(2019, 8)[1],
  "9": monthrange(2019, 9)[1],
  "10": monthrange(2019, 10)[1],
  "11": monthrange(2019, 11)[1],
  "12": monthrange(2019, 12)[1],
}

userinput=input("Which month are you looking for: ").lower()
try:
  numberDays = inputMap[userinput]
  print(f"This month has {numberDays} days")
except KeyError:
  print(f"{userinput} is not a valid option")
  print(inputMap.keys())


 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Dat Guy said:

Doing other people's homework for free is dumb.

ive already passed the homework.. im just doing this expand my knowledge of python.. >.> still learning and would like to learn how people would do it.

Link to comment
Share on other sites

Link to post
Share on other sites

31 minutes ago, steelo said:

Could you use an 2d array for this?

 

 

 

 

 

yeah see thats the problem.. i dont even know about 2d array xD we just finished with functions and now im gonna go google what this 2d array

Link to comment
Share on other sites

Link to post
Share on other sites

You need to define how many days are in each respective month.
I don't know python but there's a few ways you could do it in C#.

You could create a class with a number of the month, name of month & number of days.

 

 


public class Month {

int number;

string name;

int days;

 

Month(int n, string na, int d){

number = n;

name = na;

days = d;

}

 

 

}

 

You could also use a dictionary with an enumeration.

 

First create an enumeration definition with the 12 months. Then after this, create a dictionary where the key is the enumeration & the value is the number of days.

Dictionary<MonthEnum, int>

Link to comment
Share on other sites

Link to post
Share on other sites

Here is a snippet that I think will do what you are looking for. This is a simplified case but can get much more advanced if you wanted to get into making a class, etc
 

 

Hope that helps!

Dragoby.com

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×