0
My code isn't working.
Hej! My code isn't working, It simply gives no output, Any help appriciated https://code.sololearn.com/cbIn7RK4jH68/?ref=app
5 Antworten
+ 3
it gives output for me as 0 . It doesn't works right because choices returns a list .
+ 2
random.choices returns a list. So you end up comparing a string to a list. Instead use random.choice to return a string.
BTW I found the problem by simply adding two print statements to show the values of inp and comp.
And you could make your code more efficient by using elif and else.
+ 1
Gurseerit Singh Chahal How about this one? :-
import random
options = ("rock", "paper", "scissors")
inp = input().lower()
comp = random.choices(options)[0]
if options.index(inp) == ("scissors", "rock", "paper").index(comp):
print("You won!")
score = 1
elif inp == comp:
print("It's a tie.")
score = 0
else:
print("You lost!")
score = -1
print(f"Final {score = }")
# Hope this
+ 1
Calvin Thomas Elegant -- albeit not the most readable -- solution.
0
Hi! Gurseerit Singh Chahal
comp = random.choices(options)
If you print(comp) you can see it gives output in list.||[“rock”]||. But your input is str.
To fix this code:: add a [0] at the end of the line
comp = random.choices(options)[0]
// this will fix your code
And you have used alot of if statements which is not needed.