Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm fairly new to python and I was wondering if someone was able to help me with looping this block of code and I did looping in the past but this one involves a list element having to change every time 1 through to 10 and I don't know how I could make that change.
print ("Question 1: ")
print (questions[0])
#asks for the answer from the user
ans = int(input())
#compares the users input to the answer
if ans == eval(questions[0]):
print ("Well done, you got it correct")
#if correct grants a point to the overall score
score = score + 1
The closest way to do so while maintaining your code is the following
for index, question in enumerate(questions):
print ("Question {}: ".format(index+1))
print (question)
#asks for the answer from the user
ans = int(input())
#compares the users input to the answer
if ans == eval(question):
print ("Well done, you got it correct")
#if correct grants a point to the overall score
score = score + 1
Note that you should avoid using eval because it is unsafe. Recommended alternatives are to either make a dictionary with pre-baked questions and answers, e.g.
questions = {'What is 5 + 4?' : 9,
'What is 3 - 1?' : 2}
Or programmatically come up with questions and answers.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi all I have read through the previous answers to this question and can get the code to run. What I want to understand is why my code doesn't run.
Thanks
def collatz(number):
if number % 2 == 0:
return number // 2
elif number % 2 == 1:
return 3 * number + 1
print('Enter a number')
number = int(input())
while number != 1:
print(int(collatz(number)))
You are not updating number in your while loop so you are stuck in an infinite loop.
You should assign return value of collatz to number back, to update number.
while number != 1:
number = collatz(number)
print(number)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How exactly do I add an additional input to this if function, or atleast add a function which will allow me to give the user another option. Thank you, in advance.
ask = input ("Would you like to 1 input an existing number plate \n or 2 view a random number? 1 or 2: ")
if ask == "1":
print("========================================================================")
ask = input()("Please enter it in such form (XX00XXX): " )
If this ask = input()("Please enter it in such form (XX00XXX): " ) is actually in your code, just changing it to ask = input("Please enter it in such form (XX00XXX): " ) will fix your problem. You had one extra pair of brackets.
DISCLAIMER: This works in Python 3.x
For Python 2, use raw_input("Your question") instead of input("Your question").
As an answer to the question in your own answer:
What is wrong in this code?
ask = input ("-Would you like to 1 input an existing number plate\n--or 2 view a random number\n1 or 2: ")
if int(ask) == 1:
print("========================================================================")
while True:
ask2 = input("Please enter it in such form (XX00XXX): " ).lower()
if re.ask3("[a-z]{2}[0-9]{2}[a-z]{3}",ask2):
print("Verification Success")
break
else:
print("Verification Failed")
You have to indent the while loop once (default is 4 spaces) and everything inside of the while loop twice (default 8 spaces). When pasting a code like this, make sure to include all code needed to run. In this case, show your import re (I had to find out myself you imported that).
I hope I helped.
You are comparing an int(1) to str("1").
It should be
if int(ask) == 1:
# rest of your code
or you can use raw_input() function which returns a string value
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
However, I realized that I don't actually know how to do this myself without examining every possible combination of coins. There has to be a better way of solving this problem, but I don't know what the generic name for this type of algorithm would be called, and I can't figure out a way to simplify it beyond looking at every solution.
I was wondering if anybody could point me in the right direction, or offer up an algorithm that's more efficient.
You can try something like this:
MaxAmount = 100
TotalAmount = 0
while TotalAmount < MaxAmount:
#Here if you want it to be more precise on decimals change int(raw_input("Amount: ")) to float(raw_input("Amount: "))
EnteredAmount = float(raw_input("Amount: "))
if EnteredAmount > MaxAmount:
print "You can not go over 100"
elif TotalAmount > MaxAmount:
#You can go two ways here either just set TotalAmount to MaxAmount or just cancel the input
print "You can not go over 100"
elif EnteredAmount <= MaxAmount:
TotalAmount = TotalAmount + EnteredAmount
print TotalAmount
print "You have reached the total amount of ", MaxAmount
Could use a loop into an if - elif - else statements
e.g. populate a variable with your amount, then using this for the loop condition keep asking to take away coin amounts until you reach 0
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I apologize for the title not being clear, anyways how do I create a loop that will put numbers into a string, what i mean is (this is from the assignment im doing):
Enter number of courses:
Enter grade for course no. 1:
Enter weight for course no. 1:
Enter grade for course no. 2
......
Enter grade for course no. 7
......
Enter weight for course no. 9
So how would i do something like this, i have to create an input statement which has numbers in order (1-infinity but limited by number of courses). Thank you if you need anymore information or for me to clarify let me know.
You can use string formatting:
noc = int(input('Enter number of courses: '))
for i in range(1, noc+1):
grade = input('Enter grade for course no. {}: '.format(i))
weight = input('Enter weight for course no. {}: '.format(i))
#do something with grade and weight here.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am using Python 3.2 Just so you know what I am doing, here is the assignment:
~The function random.randint from the random module can be used to produce an integer from a range of values. For example, random.randint(1,6) produces the values 1 to 6 with equal probability. A simple tutoring program may randomly select two numbers between 0 and 12 and ask a question such as the following: What is 6 times 8? Upon receiving user response, the computer checks to see if the answer is correct and gives encouraging remarks. Write a program that will loop 10 times producing questions of this form and gives a score for the user at the end.
Here is what I have in my program:
print ("Hello. Let's begin")
for i in range (1,10):
from random import randint
x=randint (0,12)
y=randint (0,12)
print (x,"*" y,"=?")
product= int(input ("What is the product?")
if (product==x*y):
print ("Awesome! That is correct!")
else:
print ("Sorry, that is not correct, but let's try another one!")
I have everything working with all of this. It asks the user a random multiplication question and responds ten times. What I do not understand how to do is to give the user a score at the end. I'm brainstorming ideas and not much is really working. I think I would have to do something like:
score=
But I don't know how to tell the program to calculate the number of correct answers... Do I say score=number of if?
And then when I print the score I can just say:
if (score>5) :
print: ("Great job! You scored a",score,"out of ten!")
else:
print: ("Not the best score, but you can try again! You scored a",score,"out of ten.")
Or is there maybe an easier way to do this?
It seems like it would be simplest to just make a new variable ("score" or suchlike) and initialize it as 0 before the loop. Then, when you check if a user was correct, just increment it by one if it was right, and leave it alone if it was wrong.
Hope this helps!
First, set score to 0
score = 0
then in the loop, try something like
if (product==x*y):
print ("Awesome! That is correct!")
score += 1
else:
print ("Sorry, that is not correct, but let's try another one!")
the important part being the score += 1 this increases the score by one when you get a correct answer. You can the put your score > 5 in after the loop.