Python Forum
How do I do this? Switch Case?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I do this? Switch Case?
#1
Hello,

Im attempting to generate a random number as such:

import random
rnumber = random.randint(1, 10)
then I want to take that random number and have different things happen based on its outcome. In my old language I would have used a simple Select Case. I looked up switcher but im confused how to apply it here.

Thanks,
Matt
Reply
#2
Use if elif and else
if rnumber == 5:
    do this
elif rnumber == 6:
    do this
else:
    do this
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(Jun-04-2020, 09:57 PM)menator01 Wrote: Use if elif and else
if rnumber == 5:
    do this
elif rnumber == 6:
    do this
else:
    do this

ok but what do i do after else? in otherwords i have 10 option so how many if's? elif's? elses? Would you mind laying out an example of ten?
Reply
#4
Dictionaries are sometimes used to mimic a switch statement in python.
a, op, b = input('Enter Equation: ').split()
a = float(a)
b = float(b)
x = {
    '+' : a + b,
    '-' : a - b,
    '*' : a * b,
    '/' : a / b,
}[op]
print(a, op, b, '=', x)
Reply
#5
(Jun-04-2020, 10:01 PM)mstichler Wrote: in otherwords i have 10 option so how many if's? elif's? elses? Would you mind laying out an example of ten?
It's up to you,if want specific option for all ten number,then need a larger it,elif,elif...else case.
Here a example that narrow it down bye eg do one if statement for number under 5.
import random

while True:
    rnumber = random.randint(1, 10)
    if 1 < rnumber < 6:
        print('Got a number under 5')
    elif rnumber == 6:
        print('Got 6,which is close')
    elif rnumber == 7:
        print('Got 7,you win a price')
        break
    else:
        print('Got a nummer over 7')
Output:
Got 6,which is close Got a number under 5 Got a number under 5 Got a number under 5 Got a nummer over 7 Got a nummer over 7 Got a nummer over 7 Got a number under 5 Got a nummer over 7 Got 6,which is close Got a number under 5 Got a nummer over 7 Got a number under 5 Got a number under 5 Got 7,you win a price
As mention @deanhystad so is dictionaries often used for this.
I like using .get() as second parameter can catch values not found in dictionary.
import random
import sys

def win():
    return 'You hit 3 and win a price'

def switch_case(rnumber):
    return {
        7: 'Got 7,get 10 dollar',
        2: 'Got 2,which is close',
        3: win(),
        4: "Got 4,a little over",
    }.get(rnumber, f'No option for <{rnumber}>')

if __name__ == '__main__':
    while True:
        hit = input('Get a number bye press <Enter>\n')
        rnumber = random.randint(1, 10)
        print(switch_case(rnumber))
        if rnumber == 3:
            sys.exit()
Output:
Get a number bye press <Enter> No option for <8> Get a number bye press <Enter> No option for <8> Get a number bye press <Enter> Got 4,a little over Get a number bye press <Enter> Got 4,a little over Get a number bye press <Enter> No option for <6> Get a number bye press <Enter> Got 7,get 10 dollar Get a number bye press <Enter> No option for <1> Get a number bye press <Enter> You hit 3 and win a price
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  switch case not working Username0089098 1 673 Apr-09-2023, 05:49 AM
Last Post: buran
  Switch case or match case? Frankduc 9 4,387 Jan-20-2022, 01:56 PM
Last Post: Frankduc
  best way to use switch case? korenron 8 2,930 Aug-18-2021, 03:16 PM
Last Post: naughtyCat
  How to use switch/case in python? newbieguy 9 3,958 Nov-08-2019, 11:35 AM
Last Post: newbieguy
  switch limitations MuntyScruntfundle 3 2,337 Jan-27-2019, 06:11 PM
Last Post: aakashjha001
  How to write switch case statement in Python pyhelp 9 9,048 Nov-11-2018, 08:53 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020