I think the issue is you are doing random.choice(a+b)
instead of random.choice(a)
. Below works fine for me
import random
a = ["x", "y"]
b = ["z", "k"]
print("please choose between a or b")
answer = input()
if answer == "a":
print("You should check out : ", random.choice(a))
elif answer == "b":
print("You should check out : ", random.choice(b))
I believe the root of your issue is the a+b
bit under user input = 'a'. Also, you can put your prompt into your input()
statement. It seems to work fine for me when I run:
import random
a = ["x", "y"]
b = ["z", "k"]
answer = input("please choose between a or b \n>>>")
if answer == "a":
print("You should check out : ", random.choice(a))
elif answer == "b":
print("You should check out : ", random.choice(b))
Sample outputs:
please choose between a or b
>>>
a
You should check out: x
please choose between a or b
>>>a
You should check out : x
please choose between a or b
>>>
b
You should check out: k
I think the issue is you are doing anycodings_python random.choice(a+b) instead of anycodings_python random.choice(a). Below works fine for anycodings_python me,The "\n" is a new line character and is anycodings_python how I got the prompt >>> to anycodings_python show on the next line in my output,I want to make a simple app that suggests an anycodings_python artist to listen based on user input genre anycodings_python choice. I have concatenated lists(genres) anycodings_python and it can print random item from any of the anycodings_python lists using random.choice. However i want it anycodings_python to print a random item only from the list anycodings_python that user inputs, not random item from anycodings_python random list.,Tried with changing between elif and else. anycodings_python Tried making a for loop, but without much anycodings_python success.
Tried with changing between elif and else. anycodings_python Tried making a for loop, but without much anycodings_python success.
import random
a = ["x", "y"]
b = ["z", "k"]
print("please choose between a or b")
answer = input()
if answer == "a":
print("You should check out : ", random.choice(a + b))
elif answer == "b":
print("You should check out : ", random.choice(b))
I think the issue is you are doing anycodings_python random.choice(a+b) instead of anycodings_python random.choice(a). Below works fine for anycodings_python me
import random
a = ["x", "y"]
b = ["z", "k"]
print("please choose between a or b")
answer = input()
if answer == "a":
print("You should check out : ", random.choice(a))
elif answer == "b":
print("You should check out : ", random.choice(b))
I believe the root of your issue is the anycodings_python a+b bit under user input = 'a'. Also, anycodings_python you can put your prompt into your anycodings_python input() statement. It seems to work fine anycodings_python for me when I run:
import random
a = ["x", "y"]
b = ["z", "k"]
answer = input("please choose between a or b \n>>>")
if answer == "a":
print("You should check out : ", random.choice(a))
elif answer == "b":
print("You should check out : ", random.choice(b))
Sample outputs:
please choose between a or b
>>>
a
You should check out: x
please choose between a or b
>>>a
You should check out : x
please choose between a or b
>>>
b
You should check out: k
Last Updated : 19 Jul, 2021,GATE CS 2021 Syllabus
The original list is: ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String: Gsere
The original list is: ['Gfg', 'is', 'Best', 'for', 'Geeks']
Concatenated String: Gitrk
What is the most efficient way to concatenate many strings together?,You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b. Using nested scopes:,Performance My program is too slow. How do I speed it up? What is the most efficient way to concatenate many strings together? ,str and bytes objects are immutable, therefore concatenating many strings together is inefficient as each concatenation creates a new object. In the general case, the total runtime cost is quadratic in the total string length.
>>> x = 10 >>> def bar(): ...print(x) >>> bar() 10
>>> x = 10 >>> def foo(): ...print(x) ...x += 1
>>> foo()
Traceback(most recent call last):
...
UnboundLocalError: local variable 'x'
referenced before assignment
>>> x = 10 >>>
def foobar():
...global x
...print(x)
...x += 1 >>>
foobar()
10
>>> print(x) 11
>>> def foo():
...x = 10
...def bar():
...nonlocal x
...print(x)
...x += 1
...bar()
...print(x) >>>
foo()
10
11