+ 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
32 ответов
+ 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 ;-)
+ 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)
+ 1
Aarti Sharma you the probelm in the community
0
you input() twice if input is different from "hint" in if statement...
and your elif statement must check for "miss":
0
points = 100
for _ in range(4):
inp = input()
if inp == "hit":
points += 10
elif inp == "miss":
points -= 20
print(points)
0
visph i dont understand what the lines with assingments for input and “for_in range(4):” mean i havent learnt that yet
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
0
visph thank you so much. I will check the for loop lesson again
0
visph its in the next section of lessons under lists not control flow
0
really?
so, sorry...
you can do:
i = 0
while i < 4:
# the code of for loop
i += 1
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
0
provide the task description if you need more accurate help ;)
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.
0
Lis where did you seen java code in this thread???
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
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
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.
0
HK AKA MrSpace I think you mean elif x == miss