Python Forum
having an issue with nested if statements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
having an issue with nested if statements
#1
Hi,
I am trying to design a baseball game based on a dice roll for fun.

I am having an issue with nested ifs

from random import randint
manonbase={'1':'f', '2':'f', '3':'f'}
team1runs=0
team2runs=0
innings=0
outs=0

#roll=randint(1, 20)
roll = 1
if roll == 1:  #single
    if manonbase['1']=='t':
        if manonbase['2']=='t':
            if manonbase['3']=='t':
                team1runs+=1
            else manonbase['3']='t':

print (team1runs)
I keep getting an error about an invalid syntax on the else statement and pycharm tells me it expects a colon which is obviously not the issue
Reply
#2
else:
    expression, statement or code block
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
The else block is empty.
You should write the if statement in a compact form.
Nesting so much if statements is hard to read.


if roll == 1:  #single
    if manonbase['1']=='t' and manonbase['2']=='t':
        if manonbase['3']=='t':
            team1runs+=1
        else manonbase['3']='t':
            print('This else Block was empty')
# it can still improved.
# for my opinion there are still too much nested if-blocks.


Instead of using strings for the keys, you can also use integers.
Instead of using a dict, you can use a list. Currently you're using a dict
for the wrong task. It looks for me, that you want to have index access of a sequence.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
I managed to figure out the answer in post 2 thank you. I will review the post 3 part. Thanks!

I originally used numbers 0 and 1 for on and off the base but I found this confusing. t / f was just easier for me. I used a dict because I want to have a way to maintain base runner states. There are 3 bases and always there will be people on or off the base. Eventually I will build a lot of the logic into functions thanks again!

Is there a way to string along statements that involve assignment based on a conditional?

Trying this:

  manonbase['1'] = 'f' and manonbase['2'] = 't'
[python/] 

and it doesnt seem to like it 

or this 

[python]

  manonbase['1'] = 'f' & manonbase['2'] = 't'
  manonbase['1'] = 'f', manonbase['2'] = 't'

I think this answers my question

https://stackoverflow.com/questions/1674...n-one-line
Reply
#5
if manonbase['1']=='t' and manonbase['2']=='t':
The above can be simplified:
if manonbase['1'] == 't' == manonbase['2']:
Since you are checking for 't', the whole nested if block can be turned into:

if roll == 1 and all([mOB == 't' for mOB in manonbase.values()]):
    team1runs += 1
else:
    manonbase['3'] = 't'
It will work without [] but just as generator expression too.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
The problem I am running into with using all if statements is the code keeps overwriting itself. I thought I could use break to get out of it but apparently that doenst work with if statements

#roll=randint(1, 20)
roll = 1
if roll == 1:  #single
    if manonbase['1'] == 'f' and manonbase['2'] == 'f' and manonbase['3'] == 'f': #Empty bases
        manonbase['1'] = 't'

    if manonbase['1'] == 't' and manonbase['2'] == 'f' and manonbase['3'] == 'f': #Runner on 1st
        manonbase['2'] = 't'
    if manonbase['1'] == 't' and manonbase['2'] == 't' and manonbase['3'] == 'f': #Runner on 1st & 2nd
        manonbase['3'] = 't'
    if manonbase['1'] == 't' and manonbase['2'] == 't' and manonbase['3'] == 't': #Bases loaded
        team1runs += 1
    if manonbase['1'] == 't' and manonbase['2'] == 'f' and manonbase['3'] == 't': #1st and 3rd
        manonbase['1'] = 'f'; manonbase['2'] = 't'; moonbase['3'] = 'f'
        team1runs += 1
    if manonbase['1'] == 'f' and manonbase['2'] == 't' and manonbase['3'] == 'f':


    if manonbase['1'] == 't' and manonbase['2'] == 't' and manonbase['3'] == 't':
        team1runs +=1
    if manonbase['1'] == 't' and manonbase['2'] == 't' and manonbase['3'] == 't':
Reply
#7
You might want to simplify your design to reduce the number of nested if statements. Perhaps something like the following example code. I arbitrarily defined SINGLE, DOUBLE etc. to be numbers. You can define them to anything you like.
# State Machine Definitions
# Bit 0 = First Base
# Bit 1 = Second Base
# Bit 2 = Third Base
# Bit 3 = Run Scored
# Bit 4 = Run Scored
# Bit 5 = Run Scored
# Bit 6 = Run Scored

# Single = Multiply by 2 and add 1
# Double = Multiply by 4 and add 2
# Triple = Multiply by 8 and add 4
# Home Run = Multiply by 16 and add 8


FIRST = 1
SECOND = 2
FIRST_AND_SECOND = 3
THIRD = 4
FIRST_AND_THIRD = 5
SECOND_AND_THIRD = 6
BASES_LOADED = 7

SINGLE = 100
DOUBLE = 200
TRIPLE = 300
HOMER = 400

def get_new_state(old_state, runs, action):
    if action == SINGLE:
        new_state = old_state * 2 + 1
    elif action == DOUBLE:
        new_state = old_state * 4 + 2
    elif action == TRIPLE:
        new_state = old_state * 8 + 4
    elif action == HOMER:
        new_state = old_state * 16 + 8
    #    
    # Perform bitwise and on bits 3 thru 6
    runs_added = 0
    mask = 4
    for bit_number in range(3,7):
       mask *= 2
       if mask & new_state:
           runs_added += 1       
    #
    new_state = new_state & 7           
    runs += runs_added
    #
    return new_state, runs_added
    
team1runs=0
team2runs=0
new_state = 0

state = FIRST_AND_THIRD
action = TRIPLE   
print(" Old team1 state =  {}    runs = {}".format(state, team1runs)) 
state, team1runs = get_new_state(state, team1runs, action)
print(" New team1 state =  {}    runs = {}".format(state, team1runs)) 
Output:
Old team1 state = 5 runs = 0 New team1 state = 4 runs = 2
For more information about bitwise math see https://www.tutorialspoint.com/python/bi...xample.htm

Lewis
To paraphrase: 'Throw out your dead' code. https://www.youtube.com/watch?v=grbSQ6O6kbs Forward to 1:00
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Nested for loop issue always using index 0 searching1 2 2,577 Dec-30-2018, 09:17 AM
Last Post: searching1
  Nested IF statements severnon1188 1 2,610 Oct-08-2017, 10:31 PM
Last Post: ichabod801
  Nested loops, lists and if statements Liquid_Ocelot 10 8,931 Apr-23-2017, 02:02 PM
Last Post: Mekire

Forum Jump:

User Panel Messages

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