Keep getting syntax error.
I keep getting........
CODE:
answer = "b"
question = "1) Who made the game Grand Theft Auto 5??\nPlease choose from the following and write a letter: \n◘ A)Viral\n◘ B)Rockstar Games\n◘ C)Madfinger\n◘ D)Gameloft"
print(question)
guess=input().lower()
name=input("Please enter your answer: ")
if guess ==answer:
print("Correct")
score = score + 1
print("Score:")
print(score)
else:
print("Wrong")
You never use name, so why not removing it?
answer = "b"
question = "1) Who made the game Grand Theft Auto 5??\nPlease choose from the following and write a letter: \n◘ A)Viral\n◘ B)Rockstar Games\n◘ C)Madfinger\n◘ D)Gameloft\n"
print(question)
guess=input("Please enter your answer: ").lower()
if guess ==answer:
print("Correct")
score = score + 1
print("Score:")
print(score)
else:
print("Wrong")
Related
I'm still a beginner guys, so I don't know if what I'm doing is right...
In the code below I have a global Dictionary for player 1 and player 2. I then have a Function whereby player 1 chooses if he wishes to play as "x" or "o"... he/she is then assigned the choice, is there a way for me to automatically assign the opposite choice to the other player?
If this is a silly question, I apologize, I don't know
player1 = {"key1" : " "}
player2 = {"key2" : " "}
def start_game():
print('Welcome to Tic Tac Toe')
y = input("Player 1, select your marker (x/o): ")
only_choice = ['x', 'o']
if y in only_choice:
player1['key1'] = y
I'm not to confident that I am asking this question correctly but this is what I'd like to do.
In django admin, I would like to write an action that sorts the list of my contestants randomly and doesn't allow two people with the same first name to be within 4 records of eachother. So basically,
if you have John L. John C. Carey J, Tracy M. Mary T., the records would be listed like this:
John L.
Mary T.
Carey J.
Tracy T.
John C.
OR
How can I write an action that would create random groups where two people with the same name wouldn't be within the same group like so:
John L. John C. Carey J, Tracy M. Mary T. =
Group 1
John L.
Mary T.
Carey J.
Tracy T.
Group 2
John C.
Forgive me if it isn't very clear, let me know and I'll try to specify further but any help would be appreciated
EDIT:
Is this what you are referring to? I can't quite figure out how to compare the fields to see if they are the same
Model:
class people(models.Model)
fname = model.CharField()
lname = model.CharField()
group = model.IntegerField()
View:
N = 4
Num = randint(0, N-1)
for x in queryset:
x.group = Num
if group == group| fname == fname | lname == lname:
x.group = (Num + 1) % N
Your first question cannot be solved always. Just think of all contestants have the same name, then you actually cannot find a solution to it.
For the second question, I can suggest an algorithm to do that, though.
Since I do not see any restriction on the number of groups, I will suggest a method to create the least number of groups here.
EDIT: I assumed you don't want 2 people with same "First name" in a group.
The steps are
Count the appearance of each name
count = {}
for x in queryset:
if x.fname not in count:
count[x.fname] = 0
count[f.name] += 1
Find the name with the most appearance
N = 0
for x in queryset:
if count[x.fname] > N:
N = count[x.fname]
Create N groups, where N equals to the number of appearance of the name in step 2
For each name, generate a random number X, where X < N.
Try to put the name into group X. If group X has that name already, set X = (X + 1) % N and retry, repeat until success. You will always find a group to put the contestant.
from random import randint
groups = [[]] * N
for item in queryset:
X = randint(0, N-1)
while item.fname in groups[X]:
X = (X + 1) % N
groups[X].append(item.fname)
item.group = X
EDIT:
Added details in steps 1, 2, 4.
From the code segment in your edited, I think you do not actually need a definition of "group" in model, as seems you only need a group number for it.
age = 0
joke = 'What do you call a cow with no legs? Ground beef.'
myName = raw_input('Hello! What is your name?')
myAge = raw_input('Tell me your age, ' + myName + ', to hear a joke!')
if age < 6:
print('You are too young to hear this joke.')
elif age > 12:
print('You are too old to hear this joke.')
elif age == range(6, 12):
print(joke)
The code, when run, only says that I am too young to hear the joke even if I put a number higher than 6 or even higher than 12. Could you help me fix this problem?
There's no need for the last elif. Just use else without a condition.
If both age < 6 and age > 12 evaluate to false, we know that 6 ≤ a ≤ 12. There's no need to check for it.
BTW, range() returns an iterable, not something you can compare with a number. The comparison will always fail because you're comparing two things that are incompatible.
You're assigning the input into variable "myAge" and are testing variable "age" in your if statement.
Replace
myAge = raw_input('Tell me your age, ' + myName + ', to hear a joke!')
with
age = input('Tell me your age, ' + myName + ', to hear a joke!')
def main():
print "This Program will calculate the amount of parking charges by hours using a given list: "
ticket = raw_input("Please enter ticket. If lost, Please enter no")
if ticket in ['No','no','N','n']
hour = float(input("Enter total hour at parking deck: ")
while(hour <= 0 or hour > 24):
hour = int(input("Enter an integer between 1-24 (hour): "))
The code above has a syntax error at line 6 at the word while
There is a bracket/brace missing in the below line. Add the brace and the error should go away.
hour = float(input("Enter total hour at parking deck: ")
Also the if needs a colon at the end. Below are the corrected lines
if ticket in ['No','no','N','n']:
hour = float(input("Enter total hour at parking deck: "))
Statements that start a new block such as if and while need a semicolon at the end.
if ...:
...
while ...:
...
And their blocks need to be indented one level as well.
I am new (like 2 weeks) trying to learn Python 2.7x.
I am trying to do a basic program that has a user input a cost of a meal and it outputs how much it would be with a .15 tip. I want the output to look like 23.44 (showing 2 decimals)
My code:
MealPrice = float(raw_input("Please type in your bill amount: "))
tip = float(MealPrice * 0.15,)
totalPrice = MealPrice+tip
int(totalPrice)
print "Your tip would be: ",tip
print "Yout total bill would be: ",totalPrice
my output:
Please type in your bill amount: 22.22
Your tip would be: 3.333
Yout total bill would be: 25.553
You want to format your float value for printing only; use formatting:
print "Your tip would be: {:.2f}".format(tip)
print "Your total bill would be: {:.2f}".format(totalPrice)
The .2f is a formatting mini language specification for a floating point value of 2 digits after the decimal.
You need to remove the int() call to preserve those digits after the decimal. You don't need to call float() so much either:
MealPrice = float(raw_input("Please type in your bill amount: "))
tip = MealPrice * 0.15
totalPrice = MealPrice + tip
print "Your tip would be: {:.2f}".format(tip)
print "Your total bill would be: {:.2f}".format(totalPrice)
Demo:
Please type in your bill amount: 42.50
Your tip would be: 6.38
Your total bill would be: 48.88
You can further tweak the formatting to align those numbers up along the decimal point too.