My code isn't working. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

8th Aug 2021, 4:55 PM
Gurseerit Singh Chahal ✓
Gurseerit Singh Chahal ✓ - avatar
5 Answers
+ 3
it gives output for me as 0 . It doesn't works right because choices returns a list .
8th Aug 2021, 5:03 PM
Abhay
Abhay - avatar
+ 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.
8th Aug 2021, 5:15 PM
Simon Sauter
Simon Sauter - avatar
+ 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
8th Aug 2021, 6:30 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Elegant -- albeit not the most readable -- solution.
8th Aug 2021, 7:36 PM
Simon Sauter
Simon Sauter - avatar
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.
8th Aug 2021, 8:42 PM
Iftekhar Ahmed Pranto
Iftekhar Ahmed Pranto - avatar