Author Topic: Quick python problem/help  (Read 1577 times)

So, im on holiday and decided to take my laptop to do some small coding, but i'm stuck on something,

I'm creating a small game, a simple text game and I was wondering if I could make a varible true/false randomly

so for example:

If you were searching for an item, but there was a 50/50 chance of that item appearing, what would be the code for that variable to be true/false and be recognizable.

EG CODE
Code: [Select]
def monster():
    print("You search for monsters")
    command = prompt()  # It lets you type in a command
    if command == True:
        print("You find a monster")
        monsterbattle()
    if command == False:
        print("You don't find a monster")
        home()

I'm hoping the code can be somewhere else, like at the start that randomly makes it true/false somehow

/help

Code: [Select]
import random
var = random.randrange(0,2)

now var will be either 1 or 0

Code: [Select]
import random
var = random.randrange(0,2)

now var will be either 1 or 0

So instead of it being true/false it will be 0,1?

So instead of it being true/false it will be 0,1?
if you want true/fasle it would be
Code: [Select]
var = random.choice([True, False])but it does the same job

if you want true/fasle it would be
Code: [Select]
var = random.choice([True, False])but it does the same job

Cheers :)

So instead of it being true/false it will be 0,1?

Boolean

0 = false
1 = true

if you want true/fasle it would be
Code: [Select]
var = random.choice([True, False])but it does the same job

So would this randomize the variable each time it is used?

Because for some reason it keeps repeating the same thing each time I make it function

So would this randomize the variable each time it is used?

Because for some reason it keeps repeating the same thing each time I make it function

It's funny how Python works though. Once it makes it's decision, true or false, it will always stick to that decision.

Quote
import random
var4 = bool(random.getrandbits(1))
print(var4)

No matter how many times you do print(var4) it will always stick to the answer it chose first, whether it's true or false. You have to find a way to refresh the variable. By either defining it again or w/e.

Or at least that's what I'm gathering at the moment, it's weird, like C++ when it comes to integers, if you make an integer without a value, it's value will get randomly selected as whatever number the computer feels like choosing and will keep that through the whole program.

...if you set a variable it doesn't change on it's own until you set it again what are you guys talking about

...if you set a variable it doesn't change on it's own until you set it again what are you guys talking about
Yeah,

Code: [Select]

import random
var1 = random.randint(0, 1)


works fine, what are you on. Every time you call var one to random it'll refresh.
Also an example of what you want.

Code: [Select]
import random
var1 = random.randint(1, 0)
if var1 != 0:
    print ("hola, coma esta?")
else:
    print ("Muy mal")


Something to make it easier, (has comments)

Code: [Select]
import random

start = input("Do you want to search around? : ").lower() # Asks the user in text if he wants to search around, allows text. .lower means the words entered in will be lowercase,
if start == "yes" or "y": #Checks if what they entered in is yes or y, it's .lower makes their answer lowercased.
    print ("You search around...")
    var1 = random.randint(0, 1) # Random with 0 and 1 and everything in between.
    if var1 != 0: # Check if var1 doesn't equal 0
        print ("you find an item!") # If it doesn't print this
    else:
    print ("You find nothing of interest") # If it does equal 0 or anything else print this.
else:  # if the answer is anything but yes or y
    print ("You move on") # print this.
     
« Last Edit: October 12, 2013, 09:28:09 PM by Starzy »

Code: [Select]
[quote author=Kirze link=topic=243272.msg6966215#msg6966215 date=1381614366]

[code]
def monster():
    print("You search for monsters")
    command = prompt()  # It lets you type in a command
    if command == True:
        print("You find a monster")
        monsterbattle()
    if command == False:
        print("You don't find a monster")
        home()

[/quote]

By the way
Code: [Select]
def monster():
    print("You search for monsters")
    command = prompt()  # It lets you type in a command
    if command == True:
        print("You find a monster")
        monsterbattle()
    if command == False:
        print("You don't find a monster")
        home()
You don't need to define it unless you're calling it a function like

monster(), would call everything in the function monster.
Also with the if command == True:
and the if command == False:,
it won't work if you have both as if, it'll call the first if and if it isn't true nothing will happen,
you need an elif statement for the second one and so on. elif runs when the first if statement fails.
If co == blah:
elif co == blerg:[/code]
« Last Edit: October 13, 2013, 06:36:23 AM by Starzy »

yeah i'm giving up on op, he always asks this and when I solve it for him and tell him he doesn't respond and keeps asking in other threads. honestly if you need help just pm me.

I'm on holiday, I'm not always at my laptop

I'm also posting off my phone at this present time, so I can't do much