Someone who's good at Python. Halp please. No if statements.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#1  Edited By Aegon

So I've done all the questions on this assignment, but there's one left where I can't use an if statement which is making this really hard to figure out. What I have to do is create a function, let's call it months, and make it output three letter strings of the months when you input the number of the month. So if you input 4, it give you 'Apr'.

Here's where the problem comes in. If the number inputted is out of the 1-12 range, the function has to output a message telling you that you've entered a number out of the range. How do I do this without an if statement?

Avatar image for baillie
Baillie

4714

Forum Posts

37415

Wiki Points

0

Followers

Reviews: 0

User Lists: 4

#2  Edited By Baillie

I have no idea about Python, but in spreadsheets, you can do this with just by reference cells.

Avatar image for bushpusherr
bushpusherr

1080

Forum Posts

2

Wiki Points

0

Followers

Reviews: 0

User Lists: 6

#3  Edited By bushpusherr

I'm not versed in Python at all, but if it has anything equivilant to the switch statment in c or c++, you could use that with a default case for the out of range check.

Avatar image for glacialhelmnun
glacialhelmnun

58

Forum Posts

22

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#5  Edited By Aegon
Avatar image for deactivated-5ff27cb4e1513
deactivated-5ff27cb4e1513

771

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

Avatar image for baillie
Baillie

4714

Forum Posts

37415

Wiki Points

0

Followers

Reviews: 0

User Lists: 4

#7  Edited By Baillie
Month NumberMonth (Abbreviated)
User Input (1-12)"Reference to Table Below*
Column HeadColumn Head
1January
2February
3March
4April
5May
6June
7July
8August
9September
10October
11November
12December

This is kind of what I mean, is there not a way to incorporate that into Python? Like I said, I have no idea.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#8  Edited By Aegon
Avatar image for foolishchaos
FoolishChaos

515

Forum Posts

2

Wiki Points

0

Followers

Reviews: 0

User Lists: 1

#9  Edited By FoolishChaos

A switch statement is basically just another, cleaner way of doing if/else if, when you have a decent number of things to check.

If you guys haven't learned switch statements, or arrays, and your professor wants you to do this without an if statement, then its probably a python specific thing (and I'm not at all familiar with python, sorry :( ).

edit: A quick google search shows that python doesn't exactly have switch statements, but you do have dictionaries. Have you learned about those? In this case its alot like using the array method Ubersmake suggested.

Avatar image for sir_gunblade
sir_gunblade

162

Forum Posts

134

Wiki Points

0

Followers

Reviews: 4

User Lists: 1

#10  Edited By sir_gunblade

Yeah, you can do a switch/case statement in Python. What text book have you been working out of, and what chapter have you gotten to?

Avatar image for dagbiker
Dagbiker

7057

Forum Posts

1019

Wiki Points

0

Followers

Reviews: 0

User Lists: 16

#11  Edited By Dagbiker
def f(x): return { 'a': 1, 'b': 2, }.get(x, 9)
Avatar image for praab_nz
Praab_NZ

281

Forum Posts

160

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#12  Edited By Praab_NZ

@FoolishChaos

A switch statement is basically just another, cleaner way of doing if/else if, when you have a decent number of things to check.

If you guys haven't learned switch statements, or arrays, and your professor wants you to do this without an if statement, then its probably a python specific thing (and I'm not at all familiar with python, sorry :( ).

Python doesnt have a switch like Java or C# unfortunately.

Avatar image for deactivated-5ff27cb4e1513
deactivated-5ff27cb4e1513

771

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

@Aegon said:

@Ubersmake said:

Little/no Python here, but you could stuff all of your values into an array, and then use a number to get a specific index in that array: http://www.dreamincode.net/forums/topic/82900-working-with-arrays-in-python/

This is another method that we haven't learned in class and I think it would be pretty suspicious if I used it.

What have you actually gone over? Is string manipulation on there? The only other stupid-easy way I can think of doing this is to put all the months into one big string, "JanFebMarApr...", and then use a number to get a substring between ((n - 1) * 3) and (((n - 1) * 3) + 2), inclusive.

And if you're wondering where I got that equation, it's:

((Number of Month - 1 for Zero Index) * (Length of Month String)) + (Characters to end of Month String)

Of course, my math could be completely wrong, but that's the only other way I can think of doing this without if statements, or more standard data structures.

Avatar image for mwjeffcott
mwjeffcott

49

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#14  Edited By mwjeffcott

You could set up a dictionary, which maps from keys to values (like words to definitions in a book-style dictionary).

Example:

a = dict({1:"January", 2:"February", 3:"March"...........12:"December"})

You can do lookups on the dictionary of the form a[n], i.e. b = a[3], which will set b to "March"

More info: http://docs.python.org/2/library/stdtypes.html#mapping-types-dict

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#15  Edited By Aegon

Ok, maybe the answer to this is dictionaries, but I'm just realizing that one of my answers to another question is wrong and I'm struggling to figure that out now.

Avatar image for paco
paco

132

Forum Posts

21

Wiki Points

0

Followers

Reviews: 0

User Lists: 5

#16  Edited By paco

It's been a while since I've done any python, but maybe you could use a while loop to keep asking for a number if it's outside the range, like so:

num=int(raw_input("enter a number: "))
while (num > 12) or (num < 1) :
  print "please enter a number between 1 and 12"
  num=int(raw_input("enter a number: "))
 
Pretty rudimentary, but does exactly what you need without an if statement. Hope that helps.

Avatar image for praab_nz
Praab_NZ

281

Forum Posts

160

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#17  Edited By Praab_NZ

@jhardcopy said:

You could set up a dictionary, which maps from keys to values (like words to definitions in a book-style dictionary).

Example:

a = dict({1:"January", 2:"February", 3:"March"...........12:"December"})

You can do lookups on the dictionary of the form a[n], i.e. b = a[3], which will set b to "March"

More info: http://docs.python.org/2/library/stdtypes.html#mapping-types-dict

Still a problem of numbers out of range, even using the 'in' and 'not in' requires an if test to change the output.

Avatar image for rollingzeppelin
rollingzeppelin

2429

Forum Posts

8

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#18  Edited By rollingzeppelin

This works for everything but numbers from 0 to -11. This is because python interprets a negative number as the item starting from the last and going backwards in the list. This seems like a silly question, an if statement would work perfectly for this situation. I'm not sure what your prof is trying to teach here, how to impose arbitrary frustrating limitations on oneself?

def getmonth(N):

try:

months=['jan','feb','mar','apr','may','jun','jul','aug','sep','oct','nov','dec']

f = months[N-1]

return f

except:

return 'num out of range'

u = getmonth(15)

I'm not sure how you could get around the negative number problem without an if statement.

Edit: apparently you cant indent on this forum, note that the try: and except: are indented once and the lines starting with months, f, and return are indented twice.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#19  Edited By Aegon

@Praab_NZ said:

@jhardcopy said:

You could set up a dictionary, which maps from keys to values (like words to definitions in a book-style dictionary).

Example:

a = dict({1:"January", 2:"February", 3:"March"...........12:"December"})

You can do lookups on the dictionary of the form a[n], i.e. b = a[3], which will set b to "March"

More info: http://docs.python.org/2/library/stdtypes.html#mapping-types-dict

Still a problem of numbers out of range, even using the 'in' and 'not in' requires an if test to change the output.

Yeah, the out of range part is the problem.

Avatar image for praab_nz
Praab_NZ

281

Forum Posts

160

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#20  Edited By Praab_NZ

@RollingZeppelin: That's the one thing i really dislike about python, the indentation is quite hassling if you aren't used to it.

Avatar image for mwjeffcott
mwjeffcott

49

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#21  Edited By mwjeffcott

@Aegon: You could figure out what sort of exceptions doing so would cause, and handle them gracefully.

Avatar image for rollingzeppelin
rollingzeppelin

2429

Forum Posts

8

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#22  Edited By rollingzeppelin

@Praab_NZ said:

@RollingZeppelin: That's the one thing i really dislike about python, the indentation is quite hassling if you aren't used to it.

I don't mind it, you can highlight many rows of text and tab once to indent them all. Plus it makes the program look really clean.

Avatar image for praab_nz
Praab_NZ

281

Forum Posts

160

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#23  Edited By Praab_NZ

@RollingZeppelin: Thats true, but a language like Java or C can have the same tidy look without a need for consistent indentation. I just auto format all my stuff in a program like eclipse though anyway so they are roughly the same haha.

Avatar image for rollingzeppelin
rollingzeppelin

2429

Forum Posts

8

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#24  Edited By rollingzeppelin

@Praab_NZ: True, lol.

Avatar image for you_died
YOU_DIED

711

Forum Posts

0

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#25  Edited By YOU_DIED

your teacher is a jerk

Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#26  Edited By TyCobb

You can do it in 2 lines of code.... You could also do it in 1 line of code if you combine your dictionary {....} and the .get() method.

def get_month(key):
....months = {1: 'JAN', 2: 'FEB', 3: 'MAR'}
....return months.get(key, 'NOT FOUND!')
print(get_month(2))
def get_month(index):
....return {1: 'JAN', 2: 'FEB', 3: 'MAR'}.get(key, 'NOT FOUND!')

Python dictionaries have a get method that you can supply a returned value if the key is not found.

Avatar image for psylah
psylah

2362

Forum Posts

100

Wiki Points

0

Followers

Reviews: 0

User Lists: 2

Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#28  Edited By TyCobb

@RollingZeppelin said:

@Praab_NZ said:

@RollingZeppelin: That's the one thing i really dislike about python, the indentation is quite hassling if you aren't used to it.

I don't mind it, you can highlight many rows of text and tab once to indent them all. Plus it makes the program look really clean.

I love the indentation aspect of python. It's no different than having to use brackets. If anything it makes you format your code properly. There is no reason I should have to look at C# or Java code and see a line of code at the same tab level as the method brackets; that is just disgusting. Also any decent IDE will automatically put you on the correct indentation level (PyCharm is an awesome Python IDE)

Avatar image for praab_nz
Praab_NZ

281

Forum Posts

160

Wiki Points

0

Followers

Reviews: 0

User Lists: 0

#29  Edited By Praab_NZ

@TyCobb said:

@RollingZeppelin said:

@Praab_NZ said:

@RollingZeppelin: That's the one thing i really dislike about python, the indentation is quite hassling if you aren't used to it.

I don't mind it, you can highlight many rows of text and tab once to indent them all. Plus it makes the program look really clean.

I love the indentation aspect of python. It's no different than having to use brackets. If anything it makes you format your code properly. There is no reason I should have to look at C# or Java code and see a line of code at the same tab level as the method brackets; that is just disgusting. Also any decent IDE will automatically put you on the correct indentation level (PyCharm is an awesome Python IDE)

Yeah no doubt, but Python is somewhat more difficult to read when you dont have the bracketing system, yeah its less convoluted but its often hard to tell with nested operations exactly what is going on.

Avatar image for soylentgreen
SoylentGreen

287

Forum Posts

185

Wiki Points

0

Followers

Reviews: 1

User Lists: 2

#30  Edited By SoylentGreen

@Aegon said:

This is another method that we haven't learned in class and I think it would be pretty suspicious if I used it.

I'm a little curious here: how would it be suspicious?

Programming isn't about having memorized every answer to every single problem out there, it's about being able to go out and find an answer yourself. Unless your teacher/professor is a complete jerk, going out and finding a solution that hasn't been covered in class yet should just prove that you're eager to learn.

On-topic: a dictionary sounds like your best bet. Switch statements are fugly.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#31  Edited By Aegon

@TyCobb said:

You can do it in 2 lines of code.... You could also do it in 1 line of code if you combine your dictionary {....} and the .get() method.

def get_month(key):
....months = {1: 'JAN', 2: 'FEB', 3: 'MAR'}
....return months.get(key, 'NOT FOUND!')
print(get_month(2))
def get_month(index):
....return {1: 'JAN', 2: 'FEB', 3: 'MAR'}.get(key, 'NOT FOUND!')

Python dictionaries have a get method that you can supply a returned value if the key is not found.

Thanks, he barely even glazed over that in class. I'm not sure if he even mentioned get. It'll have to do.

My last problem is trying to make a function that returns a string with all its vowels multiplied by 4. I've gotten close. I got it to multiply one of the vowels, or some other crazy thing, but not all the vowels. Any ideas? Anyone? I've been working on this one question for longer than the rest of the assignment.

I wanna listen to the bombcast :(

@SoylentGreen said:

@Aegon said:

This is another method that we haven't learned in class and I think it would be pretty suspicious if I used it.

I'm a little curious here: how would it be suspicious?

Programming isn't about having memorized every answer to every single problem out there, it's about being able to go out and find an answer yourself. Unless your teacher/professor is a complete jerk, going out and finding a solution that hasn't been covered in class yet should just prove that you're eager to learn.

On-topic: a dictionary sounds like your best bet. Switch statements are fugly.

Yes the dictionary is what I'm using. Even then, we didn't even go over the get option. From what I can gather, he really wants us to do this with no outside help. Which would mean using only the book and whatever he's taught us in class.

Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#32  Edited By TyCobb

@Aegon said:

@TyCobb said:

You can do it in 2 lines of code.... You could also do it in 1 line of code if you combine your dictionary {....} and the .get() method.

def get_month(key):
....months = {1: 'JAN', 2: 'FEB', 3: 'MAR'}
....return months.get(key, 'NOT FOUND!')
print(get_month(2))
def get_month(index):
....return {1: 'JAN', 2: 'FEB', 3: 'MAR'}.get(key, 'NOT FOUND!')

Python dictionaries have a get method that you can supply a returned value if the key is not found.

Thanks, he barely even glazed over that in class. I'm not sure if he even mentioned get.

My last problem is trying to make a function that returns a string with all its vowels multiplied by 4. I've gotten close. I got it to multiply one of the vowels, or some other crazy thing, but not all the vowels. Any ideas? Anyone?

I wanna listen to the bombcast :(

Not quite sure what you are asking. You take a string ("GIANT BOMB") and then multiply the vowels by 4? Multiply what exactly; the index of the vowel in the input string?

def multiply_vowels_by_four(input):
....vowels = ['A', 'E', 'I', 'O', 'U', 'Y']
....counter = 0
....index = -1
....for char in input:
........index += 1
........if char in vowels:
............counter += index * 4
....return counter

print(multiply_vowels_by_four("GIANT BOMB"))
Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#33  Edited By Aegon

@TyCobb: multiply the vowel itself, as in

input 'vowel'

output 'vooooweeeel'

Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#34  Edited By TyCobb

@Aegon said:

@TyCobb: multiple the vowel itself, as in

input 'vowel'

output 'vooooweeeel'

Easy:

def multiply_vowels_by_four(input):
....vowels = ['A', 'E', 'I', 'O', 'U', 'Y']
....output = ''
....index = -1
....for char in input:
........index += 1
........if char in vowels:
............output += char * 4
.........else:
............output += char
....return output

print(multiply_vowels_by_four("GIANT BOMB"))
output -> GIIIIAAAANT BOOOOMB
Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#35  Edited By Aegon

@TyCobb said:

@Aegon said:

@TyCobb: multiple the vowel itself, as in

input 'vowel'

output 'vooooweeeel'

Easy:

def multiply_vowels_by_four(input):
....vowels = ['A', 'E', 'I', 'O', 'U', 'Y']
....output = ''
....index = -1
....for char in input:
........index += 1
........if char in vowels:
............output += char * 4
............else:
........output += char
....return output

print(multiply_vowels_by_four("GIANT BOMB"))
output -> GIIIIAAAANT BOOOOMB

Alright I'm gonna try it and see what I did wrong. I can already see I was close.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#36  Edited By Aegon

@TyCobb: Yup, it works. Thank you! I was indeed very close. I think the problem was that I didn't need a nested loop and could've gotten around it. Hmmm...

Also, you don't need the index in there, lol.

Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#37  Edited By TyCobb

@Aegon: No prob. It was fun.

Avatar image for aegon
Aegon

7345

Forum Posts

104

Wiki Points

0

Followers

Reviews: 0

User Lists: 3

#38  Edited By Aegon

@TyCobb said:

@Aegon: No prob. It was fun.

Are you some sort of coder by profession?

said:

def f(x): return { 'a': 1, 'b': 2, }.get(x, 9)
That was so cryptic, yo.
Avatar image for tycobb
TyCobb

2036

Forum Posts

90

Wiki Points

0

Followers

Reviews: 1

User Lists: 0

#39  Edited By TyCobb

@Aegon said:

@TyCobb said:

@Aegon: No prob. It was fun.

Are you some sort of coder by profession?

Yea. Mainly just code in C# nowadays. I don't really have the free time anymore to play with other languages, but I dabbled in Python for a little while last year and still had PyCharm installed on my system so I figured I would see what I could remember (internet was my best friend). hehe