So for some reason my random number generator just breaks my code and I want to know how to make my random generator to pick 5 numbers out of 10. Here's my code:

# Fallout Console Edition

import random

def main():

# Base Dialouge for beginning of game.

print("Hello, welcome to The Fallout Event, a Python made RPG.")

# Simple character info

character_gender = input("Enter your character's gender:")

character_name = input("Enter your character's name:")

character_health = 100

# Intro

keep_going = input("Enter '25 Cents'To Play and after dieing enter '25 Cents'. ")

print("Your beloved Vault 23, your own friends and family have cast you out due to your behavior as a, per say, loose cannon. Before being cast to the Wasteland, you were given an old Pipboy-3000 and with it you have entrusted your life. With the Pipboy you will store items to your inventory and so on.")

while keep_going == '25 Cents' :

print("After leaving glorious Vault 23, you find a crate and find five items within...")

# Failed attempt at trying to give you 5 random items from the crate but it just breaks.

for x in range(5):

random_number = random.randint(1,10)


if random_number == 1:

print("You have found a 10MM Pistol(+ 20 AP)!")

if random_number == 2:

print("You have found a Switchblade(+ 10 AP)!")

if random_number == 3:

print("You have found a Stimpak(+ 50 HP)!")

if random_number == 4:

print("You have found a tube of Wonderglue!")

if random_number == 5:

print("You have found a Coffee Mug!")

if random_number == 6:

print("You have found a Old Baseball!")

if random_number == 7:

print("You have found an Antique Globe!")

if random_number == 8:

print("You have found a Gold Platted Flip Lighter!")

if random_number == 9:

print("You have found a box of Blamco Mac and Cheese(+ 25 HP)!")

if random_number == 10:

print("You have found a Raider Chest Piece(+1 to Defense)!")






main()

Respuesta :

I checked out your code and found one important detail. Your code works fine, but the WHILE loop is never ending. You declared the while loop to keep on going while the value is "25 Cents".

The value of keep_going is a string that will never change. The while loop will then never stop executing the codes inside it.

One way of stopping this would be to add a counter and including an AND into your while loop.

Declare a variable as a counter.

ChestCounter = 1

Now in your while loop add an AND function.

while keep_going == '25 Cents' and ChestCounter < 2:

Now in line with your for loop add the ChestCounter and increment it.

ChestCounter += 1

The code will then execute the while loop and the for loop. After the for loop finishes the 5th execution, it will then increment the ChestCounter and stop the code from executing again.

ACCESS MORE
EDU ACCESS