• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Python] Logic Help with If

Status
Not open for further replies.
Level 20
Joined
Apr 14, 2012
Messages
2,901
Code:
NumberList = []
Set_Number = [COLOR=#8888c6]input[/COLOR]([COLOR=#008080]"Enter how many numbers you want to work with." [/COLOR]+ [COLOR=#008080]"[/COLOR][COLOR=#cc7832]\n[/COLOR][COLOR=#008080]"[/COLOR])
NumberMax = [COLOR=#8888c6]int[/COLOR](Set_Number)

[COLOR=#cc7832][B]for [/B][/COLOR]x [COLOR=#cc7832][B]in [/B][/COLOR][COLOR=#8888c6]range[/COLOR]([COLOR=#6897bb]0[/COLOR][COLOR=#cc7832], [/COLOR]NumberMax):
    [COLOR=#cc7832][B]if [/B][/COLOR](x + [COLOR=#6897bb]1[/COLOR]) <= (NumberMax):
        NumberString = [COLOR=#8888c6]input[/COLOR]([COLOR=#008080]"Enter a number:" [/COLOR]+ [COLOR=#008080]"[/COLOR][COLOR=#cc7832]\n[/COLOR][COLOR=#008080]"[/COLOR])
        NumberIndex = [COLOR=#8888c6]int[/COLOR](NumberString)
        NumberList.append(NumberIndex)
        x += [COLOR=#6897bb]1
        [COLOR=#8888c6][/COLOR][/COLOR][COLOR=#8888c6]print[/COLOR](x)
    [COLOR=#cc7832][B]elif [/B][/COLOR]x > NumberMax:
        [COLOR=#8888c6]print[/COLOR]([COLOR=#008080]'reached'[/COLOR]) [COLOR=#808080]# <--- This is never reached.[/COLOR]
I actually don't know how to reach the final statement, after NumberMax iterations is reached. I tried doing:
Code:
if x <= (NumberMax - 1)
But that didn't work either.

Can anyone help? Thanks in advance.
 
Level 20
Joined
Jul 14, 2011
Messages
877
You told x to start at 0 and end when it becomes NumberMax. There is no need checking that, just print reached after the loop.

You are also incrementing x an extra time.

Python:
NumberList = []
Set_Number = input('Enter how many numbers you want to work with.\n')
NumberMax = int(Set_Number)

for x in range(0, NumberMax):
    NumberString = input('Enter a number:\n')
    NumberIndex = int(NumberString)
    NumberList.append(NumberIndex)

print('reached')
 
Status
Not open for further replies.
Top