• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

[HELP] Python Problem

Joined
Apr 5, 2012
Messages
1,997
Reaction score
826
Hello,
I am trying to make a text based game which you choose a possible outcome, it will roll a dice in the game and according to the number rolled it will show the outcome.

I stumbled across a problem when trying to show some outcomes.

I got the problem when I tried adding the second possible situation
The first lot of outcomes work

Here is my code:
Code:
import random




print ("Welcome to Mission 1 of The Hitman Cheapolution")
print ("You are a in a alley in chinatown, no one is around you")
print ("Your target is the King of Chinatown")
print ("Your target walks under a crate full of wheels. You only have your fibre wire and the silver baller")
print ("A will shoot the rope holding the crate and it will crush him making you more awesome!")
print ("B will make you sneak up on him and strangle him with your fibrewire, ya sneak little thing")

choice = input ("So what will it be? Option 'A' or Option 'B':")




choice = "A"
roll = random.randint(1,20)
print("{0}".format(roll))

if roll <10:
      print ("Oh no you missed your shot and your target spotted you. game over!")

if roll >11:
      print ("Good shot, you killed your target and your awesome metre rose by 17!")




choice = "B"
roll = random.randint(1,20)
print("{0}".format(roll))

Choice A works fine when I go to run it, but when i try to test running choice B the amount rolled by the dice with show option A aswell when just trying to show Dice roll B.

Please help!
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
You meant to use the "greater than or equal to" operator,
Code:
if roll <= 10:
      print ("Oh no you missed your shot and your target spotted you. game over!")


if roll >= 11:
      print ("Good shot, you killed your target and your awesome metre rose by 17!")
 
Joined
May 23, 2008
Messages
1,071
Reaction score
574
Oh yes! i completely forget about that, but I still get my problem, I want to see the right outcome pop up when I type A or B
They both appear when I type either A or B. I don't know where I have gone wrong

You need to use an when checking their choice. Right now you're setting their choice to A, then running the A choice code, then setting their choice to B, then running the B choice code.
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
You need to use an when checking their choice. Right now you're setting their choice to A, then running the A choice code, then setting their choice to B, then running the B choice code.
He didn't use any code to tie the choice to the roll of the dice. The user pressing 'A' or 'B' has not change what the random number actually is, and thus what the actual choice is.

PHP:
import random

print ("""Welcome to Mission 1 of The Hitman Cheapolution
You are a in a alley in chinatown, no one is around you
Your target is the King of Chinatown
Your target walks under a crate full of wheels. You only have your fibre wire and the silver baller

A will shoot the rope holding the crate and it will crush him making you more awesome!
B will make you sneak up on him and strangle him with your fibrewire, ya sneak little thing
""")

choice = input ("So what will it be? A or B? (Anything else lets fate decide!):").upper()
if choice == "A":
	roll = 1
elif choice == "B":
	roll = 20
else:
	roll = random.randint(1, 20)

print("Roll: {0}\n" . format(roll))

if roll <= 10:
      print ("Oh no you missed your shot and your target spotted you. game over!")
if roll >= 11:
      print ("Good shot, you killed your target and your awesome metre rose by 17!")
The above code is what you would expect. I even added an extra choice so the program can use random if they dont pick a or b.
 
Last edited:
Joined
Apr 5, 2012
Messages
1,997
Reaction score
826
I tested your code, When you choose A it only gives you the amount 1 and B is amount 20. I want it the user to choose either A or B, the dice will roll and what ever option they chose the amount rolled by the dice will trigger the outcome of the option/
 
Joined
Jun 8, 2007
Messages
1,985
Reaction score
490
I tested your code, When you choose A it only gives you the amount 1 and B is amount 20. I want it the user to choose either A or B, the dice will roll and what ever option they chose the amount rolled by the dice will trigger the outcome of the option/
I don't understand what you want the program to do. Is A and B the options or ..? You mean like this:
Code:
import random
def doRoll(options):
    rand = random.randint(0, len(options)-1)
    print("Roll: {0}\n" . format(rand))
    print(options[rand])
def Game ():
    print ("""Welcome to Mission 1 of The Hitman Cheapolution
        You are a in a alley in chinatown, no one is around you
        Your target is the King of Chinatown
        Your target walks under a crate full of wheels. You only have your fibre wire and the silver baller

        A will shoot the rope holding the crate and it will crush him making you more awesome!
        B will make you sneak up on him and strangle him with your fibrewire, ya sneak little thing
        """)
    choice = input ("So what will it be? A or B? ").upper()
    if choice == "A":
        options = ("Oh no you missed your shot and your target spotted you. game over!",
            "Good shot, you killed your target and your awesome metre rose by 17!"
        )
    elif choice == "B":
        options = ("Oh no you tripped over your fibrewire and your target spotted you. game over!",
            "You choked your target until he passed out! Way to sneak like a champ!"
        )
    else:
        return Game();
    doRoll(options)
Game()
 
Last edited:
Experienced Elementalist
Joined
Aug 6, 2009
Messages
259
Reaction score
45
No idea why you added an input for (A, B) when they both more or less do the same thing. Anyway, I have done my take on your code, and it seems to be working the way you want it.

(I'm not sure if you're using Python 2.7.3 or the new Python 3. So just replace raw_input with input if that's the case.)

Source :

PHP:
# Python game src

from random import randint

def Intro() :

	print \
	"""
		Welcome to Mission 1 of The Hitman Cheapolution.
		You are a in a alley in chinatown, no one is around you.
		Your target is the King of Chinatown.
		Your target walks under a crate full of wheels. 
		You only have your fibre wire and the silver baller.

	Instructions :

		A will shoot the rope holding the crate and it will crush him 
		making you more awesome!

		B will make you sneak up on him and strangle him with your 
		fibrewire, ya sneaky little thing.

	"""
	
	User_Choice()
	
def User_Choice() :
	roll = randint(1,20)
	Choice = raw_input("So what will it be? Option 'A' or Option 'B': ")
	if str(Choice.upper == "A") :
		while True :
			if roll < 10 :
				print ("(You rolled a %i) Oh no you missed your shot and your target spotted you. game over!" %(roll))
				# Stop while loop
				break
			elif roll > 11 :
				print ("(You rolled a %i) Good shot, you killed your target and your awesome metre rose by 17!" %(roll))
				# Stop while loop
				break
	elif str(Choice.upper == "B") :
		while True :
			if roll < 10 :
				print ("(You rolled a %i) Oh no you missed your shot and your target spotted you. game over!" %(roll))
				# Stop while loop
				break
			elif roll > 11 :
				print ("(You rolled a %i) Good shot, you killed your target and your awesome metre rose by 17!" %(roll))
				# Stop while loop
				break
def main() :
	Intro()
	raw_input()
	
# Initialize code
main()
 
Back
Top