I want to let my script stop, first I ask when I want to stop it:
length = input("how many times " + str(num1) + str(" do you want to see?"))
(E.G. I say ‘3’ the length should be 3)
Then I want to ‘ask’ if length is the same as where it must be when it should finish:
if length == times (times is where my script is ATM)
This doesn’t work, but if I set times to a number it does work…
Even when I print both of them (print (times, length)) it shows the same (3, 3) in my console, but it doesn’t do something… this is my script:
length = input("how many times " + str(num1) + str(" do you want to see?"))
print("starting!")
time.sleep(2)
ORnum1 = num1
times = 1
while CalcMeth == "repeat":
if C == "Yes":
print(times, "times", ORnum1, "=", num1)
num1 = num1 + ORnum1
if length == times:
C = "no"
print("Reached End, stopping.")
else:
time.sleep(num2)
times = times + 1
elif C == "no":
time.sleep(2)
print ("stopped!")
sys.exit()
if length == times:
here in your code length is in str and times is in integer.
length = int(input("how many times " + str(num1) +" do you want to see?))
This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
name = input("Say your name: ")
integerr = input(name + " pick an integer ")
numberr = input(name + " pick another integer ")
This is where I get my error
if (integerr % numberr) >= 1:
print(integerr + " is divisible by " + numberr)
else:
print(integerr + " is not divisible by " + numberr)
You have two mistakes.
input always returns a string, as Simsteve7 mentioned. You can convert to an integer using int(), and convert to a string using str().
You need to check if integerr % numberr == 0, because that means that numberr divides into integerr evenly; thus integerr is divisible by numberr.
Below is a solution for Python 2:
name = input("Say your name: ")
integerr = int(input(name + " pick an integer "))
numberr = int(input(name + " pick another integer "))
if (integerr % numberr) == 0:
print(str(integerr) + " is divisible by " + str(numberr))
else:
print(str(integerr) + " is not divisible by " + str(numberr))
If you're using Python 3, you can use f-strings. That would look like this:
if (integerr % numberr) == 0:
print(f"{integerr} is divisible by {numberr}")
else:
print(f"{integerr} is not divisible by {numberr}")
Write a function called random_equation that takes as input a single parameter, the number of operators to generate in the random equation, and returns a string representing a random math equation involving the numbers 1-10 and operators +, -, *.
def random_equation(num):
result = ""
for i in range (num):
number = random.randint(1, 10)
if number == 1:
num_gen = (" 1 ")
elif number == 2:
num_gen = (" 2 ")
elif number == 3:
num_gen = (" 3 ")
elif number == 4:
num_gen = (" 4 ")
elif number == 5:
num_gen = (" 5 ")
elif number == 6:
num_gen = (" 6 ")
elif number == 7:
num_gen = (" 7 ")
elif number == 8:
num_gen = (" 8 ")
elif number == 9:
num_gen = (" 9 ")
else:
num_gen = (" 10 ")
operator = random.randint(1,4)
if operator == 1:
op_gen = (" + ")
elif operator == 2:
op_gen = (" - ")
else:
op_gen = (" * ")
math = result + num_gen + op_gen
I don't really know where to put the [i] to get it to repeat the loop since number is an integer and num_gen is the result
There are several issues here.
Your math gets replaced every time (e.g. "" + " 2 " + " - "), thus you never get to build longer sequences. You want to make result longer in each iteration: result = result + num_gen + op_gen, instead of always creating a new math with an empty result.
You never return anything. You will want to return result when the loop is finished.
You are now generating num numbers and num operators; that will produce equations like 1 + 3 *, which are a bit unbalanced. You will want to put only num - 1 pairs, and then one more number.
There are easier ways to make a string out of a number; str(num) will do what you do in twenty lines, just without the spaces.
With operator from 1 to 4, you will be generating as many * as you do + and - combined. Intentional?
import random
question = str(random.randint(-10,100)) + random.choice(op) + str(random.randint(-10,100)) + random.choice(op) + str(random.randint(-10,100))
q_formatted = question.replace('÷','/').replace('×', '*')
answer = eval(q_formatted)
print(answer)
I'm VERY new to programming (as in, in an intro class) and python. My assignment is to prompt a user to answer math questions, count the number they got correct/incorrect, calculate/display their numeric grade, and display their letter grade.
Everything seems to be working fine.. except I can't figure out how to get it to count the number of correct/incorrect answers. Any help?
def main():
name = input("What is your name? ")
correct = 0
incorrect = 0
ans = int(input("What is 4 + 5? "))
val = add(4, 5)
if(ans == val):
correct + 1
else:
incorrect + 1
ans2 = int(input("What is 20 * 6? "))
val2 = mult(20, 6)
if(ans2 == val2):
correct + 1
else:
incorrect + 1
ans3 = int(input("What is 14 - 10? "))
val3 = sub(14, 10)
if(ans3 == val3):
correct + 1
else:
incorrect + 1
ans4 = int(input("What is 30 / 5? "))
val4 = div(30, 5)
if(ans4 == val4):
correct + 1
else:
incorrect + 1
ans5 = int(input("What is 29 + 2? "))
val5 = add(29, 2)
if(ans5 == val5):
correct + 1
else:
incorrect + 1
ans6 = int(input("What is 50 - 10? "))
val6= sub(50, 10)
if(ans6 == val6):
correct + 1
else:
incorrect + 1
ans7 = int(input("What is 5 * 11? "))
val7 = mult(5, 11)
if(ans7 == val7):
correct + 1
else:
incorrect + 1
ans8 = int(input("What is 9 / 3? "))
val8 = div(9, 3)
if(ans8 == val8):
correct + 1
else:
incorrect + 1
ans9 = int(input("What is 90 - 5? "))
val9 = sub(90, 5)
if(ans9 == val9):
correct + 1
else:
incorrect + 1
ans10 = int(input("What is 412 + 5? "))
val10 = add(412, 5)
if(ans10 == val10):
correct + 1
else:
incorrect + 1
print()
print("Thanks, " + str(name) + "!")
print()
print("Correct " + str(correct))
print()
print("Incorrect " + str(incorrect))
print()
calcGrade(correct)
def add(value, value2):
return value + value2
def sub(value, value2):
return value - value2
def mult(value, value2):
return value * value2
def div(value, value2):
return value / value2
def calcGrade(correct):
grade = (correct * 100)/ 10
print("Numeric Grade " + str(grade))
if(grade > 90):
letterGrade = "A"
if(grade > 80):
letterGrade = "B"
if(grade < 70):
letterGrade = "C"
if(grade < 69):
letterGrade = "F"
print()
print("Letter Grade " + str(letterGrade))
main()
When you write correct + 1 you are evaluating what correct plus one is equal to, but you aren't updating the value stored in correct.
What you want to put instead is correct = correct + 1. Or, more succinctly, correct += 1.
The same applies to incorrect.
Every programming language has some specific features which you can use for different purposes. In Python, there is this function called eval() which is very useful for your case ...
Here is an example if its use:
In [2]: eval('2*3'), eval('12+13'), eval('30/5')
Out[2]: (6, 25, 6)
Notice that you do the same set of operations many times:
Print a prompt
Input a number
evaluate another number
see of the two numbers are equal
increment respective counters
In Python (or in many other languages), these would be done in a loop. For this, you need to create a list if things you want to loop over. The list will look like this:
In [4]: qns
Out[4]:
['4+5',
'20*6',
'14-10',
'30/5',
'29+2',
'50-10',
'5*11',
'9/3',
'90-5',
'412+5']
At this point, you can use something called list comprehension to get all the results ...
In [5]: [input('what is ' + q + '?') for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417
Out[5]: [9, 12, 34, 6, 31, 40, 50, 2, 85, 417]
Now you need to compare them with the actual values. You can actually put the comparison within the list comprehension into a single operation.
In [6]: results = [input('what is ' + q + '?') == eval(q) for q in qns ]
what is 4+5?9
what is 20*6?12
what is 14-10?34
what is 30/5?6
what is 29+2?31
what is 50-10?40
what is 5*11?50
what is 9/3?2
what is 90-5?85
what is 412+5?417
In [7]: results
Out[7]: [True, False, False, True, True, True, False, False, True, True]
In Python, it turns out that True == 1 and False == 0 for some cases. Which cases you ask? Well, that's something that comes with experience in something called duck typing. So in a couple of months, with enough experience, you will find the answer to "which cases" almost trivial. Anyway, because of this phenomenon, you can count the right answers as:
In [8]: sum(results)
Out[8]: 6
And the incorrect answers?
In [9]: len(qns) - sum(results)
Out[9]: 4
Cheers, and happy programing!
you have some mistakes with your code. Please try to this. correct +=1 or correct = correct + 1 and incorrect -=1 and incorrect = incorrect - 1
correct = correct + 1
incorrect = incorrect + 1
By doing this, the current value of the correct/incorrect answers is incremented and stored in the same variable, basically, this is a counter, for the number of correct and incorrect answers
Q: Why does my count function only count as high as one when I have multiple integers?
CODE:
import random
while True:
value = 0
count = 0
right_counter = 0
value = int(input("Enter the amount of questions would you like to answer: "))
AVG = right_counter / value
if 0<=value<=10:
for i in range(value):
numb1 = random.randint(0, 12)
numb2 = random.randint(0, 12)
answer = numb1 * numb2
AVG = right_counter / value
problem = input("What is " + str(numb1) + " * " + str(numb2) + "? ")
right_counter =+ 1
if int(problem) == answer:
print("You are Correct! Great Job!".format(right_counter))
elif int(problem) > answer:
print("Incorrect, Your answer is too high!")
elif int(problem) < answer:
print("Incorrect, your answer is too low!")
print("You got",right_counter,"out of",value,"correct, giving you an average of ",AVG,"")
break
else:
print(" Error, Please type a number 1-10 ")
This is what the output looks like:
Enter the amount of questions would you like to answer: 3
What is 1 * 8? 8
You are Correct! Great Job!
What is 11 * 11? 122
Incorrect, Your answer is too high!
What is 1 * 7? 7
You are Correct! Great Job!
You got 1 out of 3 correct, giving you an average of 0.3333333333333333
I found some help on Tutorial, but I couldn't answer my question.
bug:
right_counter =+ 1
This is (insidiously) equivalent to
right_counter = 1
you probably meant
right_counter += 1
You probably also want to address the logic issue that right_counter is incremented regardless of the correctness of the answer.