Hello again everyone. I am creating a math game sort of...I dont know if they call it a case or switch statement but I want one in both my Menus. Also I am wondering if you can create an if statement or case statement that will support two variables...for example: if a == 2 and b == 3...THanks for your help and please respond as quick as possible...

by the way here is my code I would like you to fix with case statements:

import random
def difficult_menu():
    print("""Difficulties:
    1.) - Easy
    2.) - Medium
    3.) - Hard""")
    return int(raw_input("Choose a difficulty"))

def type_menu():
    print("""Types of Math:
    1.) - Add
    2.) - Subtract
    3.) - Divide
    4.) - Multiply""")
    return int(raw_input("Choose one of the choices"))
def main():
    x = difficult_menu()
    type_menu()
main()

Recommended Answers

All 4 Replies

Usually is to have sequence of values or dictionary, functions if necessary

from __future__ import division
from operator import *

case_func = [('+', add), ('-', sub), ('/', truediv), ('*', mul)]

def type_menu():
    print("""Types of Math:
    1.) - Add
    2.) - Subtract
    3.) - Divide
    4.) - Multiply""")
    return int(raw_input("Choose one of the choices. "))-1

selection = type_menu()
print '3 %s 4 = %s' % (case_func[selection][0], case_func[selection][1](3,4))

This might help you ...

import random

def difficult_menu():
    print("""Difficulties:
    1.) - Easy
    2.) - Medium
    3.) - Hard""")
    return int(raw_input("Choose a difficulty: "))

def type_menu():
    print("""Types of Math:
    1.) - Add
    2.) - Subtract
    3.) - Divide
    4.) - Multiply""")
    return int(raw_input("Choose one of the choices:"))

def pick_random_integer(difficulty):
    # replace multiple if/else with dictionary
    return {
    1 : random.randrange(1, 10),
    2 : random.randrange(10, 100),
    3 : random.randrange(100, 1000)
    }.get(difficulty)    

def main():
    difficulty = difficult_menu()
    random_number1 = pick_random_integer(difficulty)
    random_number2 = pick_random_integer(difficulty)
    print(random_number1, random_number2)
    math_types = type_menu()

main()

To test the above code just double cick on the code area to select the code, right click to copy the selected code, and then paste it into your favorite editor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.