Python Forum
Need help with basic code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Need help with basic code (/thread-1964.html)



Need help with basic code - Random - Feb-07-2017

I'm very new to this sort of thing, so sorry if this is a dumb question

if (n):
        print(random.choice(a))

if y:
        print (random.choice(t))
Even if I answer with something from list N, it will print an answer from both list a and T, what am I doing wrong?


RE: Need help with basic code - j.crater - Feb-07-2017

It would be easier to help if you posted all of the code, prefereably iniside code tags for readability.
There are variables (n, a, y, t) that we can only guess what they are.

If statement checks if the expression evaluates to True. If "n" and "y" are lists and you want to check if "element" is in the list, then you use:
"if element in n:"


RE: Need help with basic code - Random - Feb-07-2017

import random

t=["Ohhhh, thank you my bro!","chur brotha","chur","shot cuz","shot cuzzie"]
y=["yes","yea bro","yea","yep","sure","sure thing","no problem","no worries"]
a=["what a racheat fella","racheat","aw come on cuz",
"bro, I gave you my pie once"]
n=["no","nah","nuk"]
g=["Kia ora bro!","chur my bro!","yo my bro!"]
q=[" wanna get some maccas?"," wanna go grab a pie?"," can I have a sip of your V?"," what you been up to?"," can you drive me to KFC?",
"is it all good if I crash here?"]
x=input (random.choice(g) + random.choice(q))
if (n):
        input(random.choice(a))
if (y):
        input(random.choice(t))
thanks in advance


RE: Need help with basic code - j.crater - Feb-07-2017

I assume that in places where you use "input", you actually want to "print".
After (random.choice(g) + random.choice(q)) you'd better store the answer in a variable, e.g.
answer = input()
And then check whether answer is in "n" or is it in "y"


RE: Need help with basic code - Random - Feb-07-2017

I'm sorry, but I don't fully understand what you mean, would it be alright for you to provide an example of what I should do?


RE: Need help with basic code - buran - Feb-07-2017

First of all, please use python code tags around your code
as to your question - non-empty list always evaluates to True, so both your if conditions are always true.

if x in n:
# do something here
elif x in y:
#do something else here



RE: Need help with basic code - tannishpage - Feb-08-2017

I think you want to do something like this. 


x=input(Blahblah)
if x in n:
   something
elif x in y: 
    something