+ 4

I need help with my code

Input (str()) Points = 100 while points > 4 : if input() == "hit" : points += 10 elif input() == "hit" : points -= 20 print(points) It keeps saying some sort of error while reading the lines with the if statements. What’s wrong and how can I fix it

14th Jul 2021, 11:05 AM
Eren Ozer
32 Antworten
+ 11
There are some 2 errors that I can see in your code: - input(str()): Variable not declare which gives the error in the if condition. You could use; Useranswer = str(input()) #No need to convert input to string since it is already in that data type by default ig. - while points > 4: Not really needed. A while true: loop(with a stop condition such as in the sample code below) or athe if condition is enough. #correct answers. You can add more if you wish wordlist = ["hit"] points = 100 #We define our loop that will iterate through our answers in the wordlist list for answer in wordlist: useraswr = input("Enter answer: ") #user enters answer if useraswr == answer: points += 10 else: points -= 20 print(points) lastpost = len(wordlist) - 1 post = int(wordlist.index("answer")) if post == lastpost: break little bit complicated but efficient ;-)
15th Jul 2021, 2:33 PM
Mendjemo Ngangom Gerard Ledoux
+ 2
#I guess you want something like this; input = input() #Need to assign the input to a variable. Also note that input() already returns "string" data type. points = 100 #Stick to either capitalized or lower case. Variable names are case sensitive. while points > 4: #Please check the indentations here. I used 3 spaces for tab. if input == "hit": points += 10 elif input = "" : #Please check the second input option points - =20 print(points)
16th Jul 2021, 9:32 AM
Emmanuel K
Emmanuel K - avatar
+ 1
Aarti Sharma you the probelm in the community
16th Jul 2021, 11:37 AM
Steven
0
you input() twice if input is different from "hint" in if statement... and your elif statement must check for "miss":
14th Jul 2021, 11:08 AM
visph
visph - avatar
0
points = 100 for _ in range(4): inp = input() if inp == "hit": points += 10 elif inp == "miss": points -= 20 print(points)
14th Jul 2021, 11:14 AM
visph
visph - avatar
0
visph i dont understand what the lines with assingments for input and “for_in range(4):” mean i havent learnt that yet
14th Jul 2021, 11:16 AM
Eren Ozer
0
_ is just a valid variable name often used when variable is not used elsewhere... for var in range(4) is a for loop wich you should have learned before while loop (and easier/shorter to handle) inp = input() store the input provided by user in 'inp' variable (to be able to check twice equality -- against "hit" and against "miss") you maybe could assume that input are ever valids, and do: for _ in range(4): if input() == "hit": points += 10 else: points -= 20
14th Jul 2021, 11:22 AM
visph
visph - avatar
0
visph thank you so much. I will check the for loop lesson again
14th Jul 2021, 11:23 AM
Eren Ozer
0
visph well i just checked there is no for loop lesson in the python begineers course
14th Jul 2021, 11:24 AM
Eren Ozer
0
visph its in the next section of lessons under lists not control flow
14th Jul 2021, 11:25 AM
Eren Ozer
0
really? so, sorry... you can do: i = 0 while i < 4: # the code of for loop i += 1
14th Jul 2021, 11:28 AM
visph
visph - avatar
0
visph but would that code work for this circumstanice since i need to add or maybe subtract from 100. The proper breif was starting with 100 points and a hit = 10 plus points and a miss = 20 points deducted and in the final print it should show the total
14th Jul 2021, 11:30 AM
Eren Ozer
0
provide the task description if you need more accurate help ;)
14th Jul 2021, 11:37 AM
visph
visph - avatar
14th Jul 2021, 11:40 AM
Eren Ozer
0
Good afternoon, It looks as if your code is figting itself. The only input that can be passed would be “hit” which adds 10 points, and also subtracts 20 points.
14th Jul 2021, 11:35 PM
Steven
0
Lis where did you seen java code in this thread???
15th Jul 2021, 6:01 AM
visph
visph - avatar
0
You can use a variable to get input and then compare that variable in if case. points=100 for i in range(4): String=input().strip() if String=="hit": points+=10 elif String=="miss": points-=20
15th Jul 2021, 8:57 AM
Swetha Lakshme S
Swetha Lakshme S - avatar
0
U cant use while that when you are going to do a conditition instead of while do for i in range (4) then add a variable with value Then u do condition and do the athers steps
15th Jul 2021, 9:56 AM
Mxddy_Yt
Mxddy_Yt - avatar
0
I am trying to understand the algorithm behind this. What is it that you want your code to do? The if statement and the elif statement have the same boolean expression. Also I prefer u assign the input to a certain variable. Eg: name = input("Enter your name"), the parameter may blanket if u want. Also create a condition whereby the program under the while loop will stop looping. Eg: print("""Enter any items: (Enter D if done)""") While True: items = input() if items == "D": break Also be carefull of indentations, they are very important for the compiler. Hope this helps, if u have more questions, feel free to ask.
15th Jul 2021, 11:22 AM
Awonke Mnotoza
Awonke Mnotoza - avatar
0
HK AKA MrSpace I think you mean elif x == miss
15th Jul 2021, 4:27 PM
Nwalozie Elijah
Nwalozie Elijah - avatar