Why does the choice attribute (python) not work? - python-3.x

I have been trying to build a randomized greeting program, so far I got:
import random
a = ("Hi!", "How are you?", "You Good?")
b = random.choice(a)
print(b)
It used to work, but now it just says that there are no attributes for .choice, can someone help?
Edit1: I think my Python is broken...
Edit2: Wait it's fixed now...

Rename your own script to something different than random.py.

Your code is correct. Here are some step to refine or rebuild you code:
Rename the file and try again.
In random.choice(a). 'a' should be a List, Tuple, or String only.
try with square brackets [] instead of small bracket (). Like a = ["Hi!", "How are you?", "You Good?"]
Update your Python with the latest version.

Related

is this code, right? if there is an error, could you guide?

i am a complete noob to programming and i got an assignment from the online course where I am learning, mine also gives the output, but it is not same as the instructor's method. This works for me and this method was easy for me.
but i am not sure is this the right method or had i done something wrong? could someone help?
I was not allowed to use choice()
**
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
a=len(names)
random_name=random.randint(0,a)
print(f"{names[random_name]} is going to pay the bill")
**
Welcome!
First of all you need to describe what exactly your problem and what is the problem you are facing.
It looks like you even have not tried to run that code. I will recommend an online interpreter to you to test your code on. You may use that
https://www.online-python.com/
Secondly "import" statement must be in lower case not "Import"
Finally the code works only incase of the string (names) is separated by a comma followed by space ", " same as the split string used. for example "a,b,c" is not going to work, but "a, b, c" does
random.randint(0,a) (a is included; may be returned) so it must be a-1 to avoid IndexError: list index out of range
Fixed code
import random
names_string = input("Give me everybody's names, separated by a comma. ")
names = names_string.split(", ")
a=len(names)
random_name=random.randint(0,a-1)
print(f"{names[random_name]} is going to pay the bill")
no, the code is not right.The error can be solved by replacing "Import" by "import".

Can I make Python check my list to see if user input contains strings in my list using if statements? If not are there any alternatives?

Beginner with Python but I wanted to use a list to contain multiple potential string responses that the user may give in the input.
def Front_Door():
print("Welcome to the Party Friend!")
Emotional_state = input("How are You Today? ")
Positive_Emotion = ["good", "fine", "happy"]
I tried to use an if statement to get python to check through my list to see if the input contained any of the strings listed in the e.g. I gave.
if Positive_Emotion in Emotional_state:
print("That's Great! Happy to have you here!")
The code still manages to prompt me for Emotional_state but it just repeats the question one more time, if I respond with one of then strings I've listed again it gives me this error:
if Positive_Emotion in Emotional_state:
TypeError: 'in <string>' requires string as left operand, not list
I'm guessing there is a method to make Python search through my list of strings and cross reference it with my inputs and give me the response I want?
Any help is appreciated :).
You are checking if the whole list is in the string!
What you probably want to do is check if any of the items in the list are in the string.
Something like:
if any( [emotion in Emotional_state for emotion in Positive_Emotion] ):
print("That's Great! Happy to have you here!")
This will check each emotion in the list, and if any of them are in the string it will return True.
Hope this helps.
I'm all too late to the party, but here are my two cents.
You only need Positive_Emotion and Emotional_state to switch places in your if statement, so that it becomes:
if Emotional_state in Positive_Emotion:
print("That's Great! Happy to have you here!")

python error-can only join an iterable

I am making a program in pygame and I am trying to create with buttons where the user can enter their name. So basically when ever they click on the letter, then it puts that letter into a list. I recreated with about the same variables as in my game and got the same error. So if we can fix this, then I can fix my game. I am using functions so that is why you see the global thing as that is needed. Here is the code. I have looked at other forums btw but nothing that is the same as mine.
import glob
unknown=[]
def bob():
global unknown
a=('a').upper()
unknown=unknown.extend(a)
unknown=','.join(unknown)
bob()
Again, this is basically what is in my code except for the name of the function and the name of the list, other than that it is the same. The name doesn't matter though. Thanks in advance.

How to get a 01 string through input()?

I want to get a string which merely consists of 0 and 1.
Since raw_input() was renamed to input() in Python 3, I wrote:
buf = str(input())
print(buf)
However,when I tried '00101', buf surprisingly turned out to be '65'.
Why this happened and how to prevent this?
As a beginner in Python, it confuses me so much.
update: Problem solved. See my answer below.
i try this statement in python3 and output was 00101
btw: output from function input() is string, you dont need call str() on input()
but if you run input() in python (python 2.7) you get output 65
Problem solved. I found that I forgot to remove Python 2.7 directory from environment variables. Furthermore, a number begins with zero is considered as an octal one. That's why I got '65' for '00101' in Python 2. Thank you all.

Python3.4 Anaconda: Input() Function Broken?

I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.

Resources