In this scenario, I have two issues;
1) Why is selection 4 not taking me to exit the program variable?
2) Why is selection 2 telling me I went backward?

Thank you in advance!

#start and display menu
def main():
#fuel
fuelUsed = 0
fuel = int(200)
feet = int(0)
#ammo
global ammo
ammo = int(5)
global used
used = int(0)
#movement
block = ''
dist = 0
move = int(0)
#Menu
choice = int(0)
endProg = 'no'
print 'Menu Selections: '
print '1 - Fire Weapon '
print '2 - Move Forward '
print '3 - Move Backward '
print '4 - Exit The Program'
while endProg == 'no':
if ammo < 0:
ammo = int(0)
if used > 5:
used = int(5)
print fuel,'fuel,',fuelUsed,'used.'
print ammo,'ammo,',used,'used.'
#checks selection
choice = raw_input('Enter your selection:')
if choice == '1' or choice == '2' or choice == '3' or choice == '4':
if int(choice) == 1:
fire(ammo)
ammo = ammo - 1
used = used + 1
elif int(choice) == 2 or 3:
if choice == 2:
dir = 'forward.'
else:
dir = 'backward.'
print('Are there any objects in the way?')
block = raw_input('Y or N:')
if block == 'Y' or block == 'y':
objLoc = int(input('Enter the distance, in feet, the object is:'))
feet = input('How far do you want to move?')
if feet <= objLoc:
if not feet > fuel:
fuel = fuel - feet
fuelUsed = fuelUsed + feet
else:
print 'Not enough fuel to move that far.'
else:
print 'There is an object in the way.'
elif block == 'N' or block == 'n':
feet = input('How far do you want to move?')
if not feet > fuel:
fuel = fuel - feet
fuelUsed = fuelUsed + feet
print 'Successfully moved',feet,'feet',dir
else:
print 'Not enough fuel to move that far.'
else:
print 'Error: Invalid selection.'
elif int(choice) == 4:
print 'Goodbye!'
exitProg()
else:
print 'Error: Invalid selection.'
#fire ammo
def fire(ammo):
if ammo > 0:
dist = raw_input('Enter the number, in feet, the enemy is:')
if dist <= '20':
print 'The enemy had been destroyed.'
return
elif dist <= '40':
print 'The enemy has been partially disabled.'
return
else:
print 'The enemy was not harmed.'
return
elif ammo <= 0:
print('Out of ammo!')
#exit program
def exitProg():
print 'Y or N:'
true = raw_input('Do you wish to exit this program?')
if true == 'y' or true == 'Y':
quit()
elif true == 'n' or true == 'N':
print 'Welcome back.'
return
else:
print 'Error: Invalid selection.'
#call main
main()

Recommended Answers

All 3 Replies

Why are you writing everywhere constants with int()? If you want so many globals you better move the variables in beginning of the program.

Reason for '4' not working is your test:

elif int(choice) == 2 or 3

or 3 is always true as 3 is not 0==False and or statement is True if one of the values is not False.

Probably you want to use:

elif choice in '23':

'2' is bringing you backward as it is not equal to integer number 2.

Please forgive me for I am new to all this.
So you are saying in line
elif int(choice) == 2 or 3
you are suggesting to change it to
elif choice in '23'

Yes and also use the in test with input numbers as string, not to convert input to number ( if choice in '1234': etc)

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.