Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with basic code
#1
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?
Reply
#2
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:"
Reply
#3
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
Reply
#4
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"
Reply
#5
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?
Reply
#6
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
Reply
#7
I think you want to do something like this. 


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


Possibly Related Threads…
Thread Author Replies Views Last Post
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,999 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Writing a basic shift code djwilson0495 2 2,262 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,444 Sep-13-2019, 03:28 PM
Last Post: ichabod801
  basic code help chche5 4 5,431 Feb-17-2018, 10:24 PM
Last Post: chche5
  Basic Doubt in code prateek3 6 4,894 Aug-19-2017, 01:05 PM
Last Post: ichabod801
  Basic code help crawlinkingsnake 2 3,482 Jan-16-2017, 11:03 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020