Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 days ago.
Improve this question
a question from an assignment is as follows:
*
The voting for a company chairman is recorded by entering the numbers
1 to 5 at the keyboard, depending on which of the five candidates
secured a vote. Enter 0 to indicate that all votes have been input.
Write a program that will count the number of votes for each of the
five candidates. Once all the votes have been entered, print the total
for each candidate and the total of all the invalid votes.
My first attempt:
candidate_A = 0
candidate_B = 0
candidate_C = 0
candidate_D = 0
candidate_E = 0
spoiled_votes = 0
total_votes = 0
while True:
choice = int(input())
if choice == 0:
break
elif choice == 1:
candidate_A += 1
elif choice == 2:
candidate_B += 1
elif choice == 3:
candidate_C += 1
elif choice == 4:
candidate_D += 1
elif choice == 5:
candidate_E += 1
else:
spoiled_votes += 1
continue
print(f"Candidate A:{candidate_A}")
print(f"Candidate B:{candidate_B}")
print(f"Candidate C:{candidate_B}")
print(f"Candidate D:{candidate_B}")
print(f"Candidate E:{candidate_B}")
print(f"Invalid:{spoiled_votes}")
This attempt failed as the variables weren't incrementing.
My second and more frustrating attempt which turned out to be correct:
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
while True:
choice = int(input())
if choice == 1:
a += 1
elif choice == 2:
b +=1
elif choice == 3:
c += 1
elif choice == 4:
d += 1
elif choice == 5:
e += 1
if choice >= 6:
f += 1
continue
if choice == 0:
break
print(f'candidate A:{a}')
print(f'Candidate B:{b}')
print(f'Candidate C:{c}')
print(f'Candidate D:{d}')
print(f'Candidate E:{e}')
print(f'Invalid:{f}')
It's driving me mad that I can't figure out what the difference is?
You put:
print(f"Candidate A:{candidate_A}")
print(f"Candidate B:{candidate_B}")
print(f"Candidate C:{candidate_B}")
print(f"Candidate D:{candidate_B}")
print(f"Candidate E:{candidate_B}")
They all say "B", except for the first line.
Related
What is wrong with this cycle, please that the output is
0
One
1
One
2
One
3
One
4
One
5
One
for i in range(6):
print(i)
if i == 0 or 2 or 4:
print('One')
else:
print('Two')
I would expect alternate printing of One and Two. Many thanks.
Make the below changes in your if statement:
for i in range(6):
print(i)
if i == 0 or i == 2 or i == 4:
print('One')
else:
print('Two')
Your if i == 0 or 2 or 4: is equivalent to if (i == 0) or 2 or 4: and hence will compute as always true.
I've been trying to figure out how to add a score to this for ages. I'm still not sure ;( sorry I'm new to coding.
this is the code for the main part
while roundsPlayed > 0:
chips_left = MAX_CHIPS
n = 1
rounds = 1
while chips_left > 0:
while True:
print("Number of chips left {}".format(chips_left))
if n%2 == 0:
print("{} How many chips would you like to take (1 - 3): ".format(playerTwo))
else:
print("{} How many chips would you like to take (1 - 3): ".format(playerOne))
try:
chipsTaken = int(input())
if chipsTaken < 1 or chipsTaken > 3:
print("Please enter a valid number from 1 to 3!")
else:
break
except:
print("Thats not even a number brotherman")
n += 1
chips_left = chips_left - chipsTaken
roundsPlayed = roundsPlayed - 1
rounds += 1
I would like to implement error handling so that if the user puts in anything besides an integer they get asked for the correct input. I believe try/except would work but I am wondering how I can get it to check for both a correct number within a range and ensuring there are no other characters. I have pasted my code below for review.
Thanks!
# Rock Paper Scissors
import random as rdm
print("Welcome to Rock/Paper/Scissors, you will be up against the computer in a best of 3")
# game_counter = 0
human_1 = input("Please enter your name: ")
GameOptions = ['Rock', 'Paper', 'Scissors']
hmn_score = 0
cpt_score = 0
rps_running = True
def rps():
global cpt_score, hmn_score
while rps_running:
hmn = int(input("""Please select from the following:
1 - Rock
2 - Paper
3 - Scissors
\n""")) - 1
while not int(hmn) in range(0, 3):
hmn = int(input("""Please select from the following:
1 - Rock
2 - Paper
3 - Scissors
\n""")) - 1
print('You Chose: ' + GameOptions[hmn])
cpt = rdm.randint(0, 2)
print('Computer Chose: ' + GameOptions[cpt] + '\n')
if hmn == cpt:
print('Tie Game!')
elif hmn == 0 and cpt == 3:
print('You Win')
hmn_score += 1
elif hmn == 1 and cpt == 0:
print('You Win')
hmn_score += 1
elif hmn == 2 and cpt == 1:
print('You Win')
hmn_score += 1
else:
print('You Lose')
cpt_score += 1
game_score()
game_running()
def game_score():
global cpt_score, hmn_score
print(f'\n The current score is {hmn_score} for you and {cpt_score} for the computer \n')
def game_running():
global rps_running
if hmn_score == 2:
rps_running = False
print(f"{human_1} Wins!")
elif cpt_score == 2:
rps_running = False
print(f"Computer Wins!")
else:
rps_running = True
rps()
To answer your question, you can do something like the following
options = range(1, 4)
while True:
try:
choice = int(input("Please select ...etc..."))
if(choice in options):
break
raise ValueError
except ValueError:
print(f"Please enter a number {list(options)}")
print(f"You chose {choice}")
As for the a code review, there's a specific stack exchange for that, see Code Review
I am able to get the code work good when the compound statement is changed to
if a % 2 == 0 and b % 2 == 0:
But as I am in learning phase could someone please guide me in explaining the error in the original code.
exm_list = [(4,8),(1,2),(4,5),(6,7),(10,20),(3,5),(3,2)]
for a,b in exm_list:
if a and b % 2 == 0:
print(f'{a,b} are the even numbers')
else:
print(f'one of {a,b} is the odd number')
enter image description here
The issue is that you are not asking anything for the condition for 'a'. What you should state is the following:
exm_list = [(4,8),(1,2),(4,5),(6,7),(10,20),(3,5),(3,2)]
for a,b in exm_list:
if a % 2 == 0 and b % 2 == 0:
print(f'{a,b} are the even numbers')
else:
print(f'one of {a,b} is the odd number')
Let me know.
In you case
if a and b % 2 == 0:
is equivalent to
if bool(a) and bool(b % 2 == 0):
a is an integer so bool(a) is True if a is not 0
I am making a Dichotomous Key program where it asks questions to determine the name of the creature that is in question. Here is how it looks like right now:
step = 0
yes = ["y", "yes"]
no = ["n", "no"]
while step == 0:
q1 = input("Are the wings covered by an exoskeleton? (Y/N) ")
q1 = q1.lower()
if q1 in yes:
step += 1
elif q1 in no:
step += 2
else:
print("Huh?")
How would I put the if and else statement into a function so that I can reuse it for every question asked and change the step variable?
-Thanks
This is a working example:
step = 0
def update_step(q):
yes = ["y", "yes"]
no = ["n", "no"]
global step
if q in yes:
step += 1
elif q in no:
step += 2
else:
print("Huh?")
while step == 0:
q = input("Are the wings covered by an exoskeleton? (Y/N)")
update_step(q.lower())
print(step)
But i don't think it's a good way to solve the problem
UPDATE:
I like simplicity, that's why I try to get rid of state whenever I can. For example, I would write it this way:
total_steps = 0
def is_yes(answer):
return answer in ["y", "yes"]
def is_no(answer):
return answer in ["n", "no"]
def get_steps(answer):
if is_yes(answer):
return 1
elif is_no(answer):
return 2
return 0
while True:
answer = input('question? ')
steps = get_steps(answer.lower())
if steps == 0:
continue
total_steps += steps
break
print(total_steps)
you can make it better using more advanced techniques, but let's keep it simple :)