Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Nerd Stuff: How can I get this program to run?

+3 votes
asked Mar 10, 2022 by Joey (540 points)
Ok so I'm a nerd and I'm working on a tool program to help out with my Dungeons and Dragons group. How can I get this modifier program to actually print? I ran the program and, while it didn't give me an error message, it didn't print the modifier like I wanted.

score = int(input('Enter score:'))

if score == range(1,3):

print('Modifier is: -4')

if score == range(3,5):

print('Modifier is: -3')

if score == range(5,7):

print('Modifier is: -2')

if score == range(7,9):

print('Modifier is: -1')

if score == range(9,11):

print('Modifier is: 0')

if score == range(11,13):

print('Modifier is: +1')

if score == range(13,15):

print('Modifier is: +2')

if score == range(15,17):

print('Modifier is: +3')

if score == range(17,19):

print('Modifier is: +4')

if score == range(19,21):

print('Modifier is: +5')

if score == range(21,23):

print('Modifier is: +6')

In the program it is indented correctly.

3 Answers

0 votes
answered Mar 14, 2022 by Peter Minarik (84,720 points)

Your score shouldn't be checked if it is a range, but rather if it is within a range:

if score in range(1,3):
commented Mar 16, 2022 by Joey (540 points)
I feel completely dumb XD thanks
commented Mar 29, 2022 by Joey (540 points)
Ok so now at the end of my output it not only has the modifier printed but underneath it it is printing None. What part of the code is doing that?
commented Mar 29, 2022 by Peter Minarik (84,720 points)
Can you save your project and share it? Look for the buttons on the right side of the RUN button to save then share your project.
0 votes
answered Mar 15, 2022 by Rohit Baghel (180 points)

You have to use else id condition in this type of program...

score = int(input('Enter score:'))

if score in  range(1,3):

  print('Modifier is: -4')

else if score in range(3,5):

   print('Modifier is: -3')

else if score in range(5,7):

    print('Modifier is: -2')

else if score in range(7,9):

    print('Modifier is: -1')

else if score in range(9,11):

    print('Modifier is: 0')

else if score in range(11,13):

    print('Modifier is: +1')

else if score in range(13,15):

     print('Modifier is: +2')

else if score in range(15,17):

     print('Modifier is: +3')
 

else if score in range(17,19):

    print('Modifier is: +4')

else if score in range(19,21):

   print('Modifier is: +5')

else if score in range(21,23):

    print('Modifier is: +6')
 

commented Mar 16, 2022 by Joey (540 points)
sounds legit, thanks for your help :)
commented Mar 16, 2022 by Peter Minarik (84,720 points)
Actually, he doesn't *have to*. The reason is that the ranges are distinct, they do not overlap so it will work correctly without if-else pairs just having individual if statements.

That being said, yes, using if-else pairs to keep checking for alternative scenarios is a good practice to prevent multiple cases being executed unintentionally (and also a bit of performance gain not to check every other case unnecessarily).
0 votes
answered Mar 23, 2022 by Sam (1,310 points)
saying if it equals a range wont work u have to see if it is IN the range because technically range(3) equals 1,3 it only accesses each value  when u tell it to
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...