Python Score Counter For Controlled Test [closed] - python

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 8 years ago.
Improve this question
I have a controlled assessment in roughly seven hours that requires me to build a program that can keep score of a competition. In this competition there are 4 teams and 5 games. I need a program that will give 1 point to teams in the event of a tie, 2 points for a home win and 3 points for an away win. I also need it to display an error message or something when an incorrect team number has been entered. Can anyone help? It also needs to be in python.
I've only been coding for a few weeks and i have no idea what I'm doing. I am trying to figure out how to print out a statement when an incorrect team number is entered without it counting within the loop
My code so far:
schoolnumber = [1,2,3,4]
homescores =[0,0,0,0]
awayscores=[0,0,0,0]
for counter in range(0, 5):
whohost = int(input("Who hosted the game? "))
whowins = int(input("Who was the winner? "))
if whohost == whowins:
homescores[whowins-1] += 2
else:
awayscores[whowins-1] += 3
print(homescores)
print(awayscores)

for counter in range(0, 5):
while True:
try:
whohost = int(input("Who hosted the game? "))
whowins = int(input("Who was the winner? "))
if whohost in schoolnumber and whowins in schoolnumber:
break
else:
raise ValueError()
except ValueError:
print('Please enter a valid number from the list {}'.format(schoolnumber))
if whohost == whowins:
homescores[whowins-1] += 2
else:
awayscores[whowins-1] += 3
This will loop over the two input statements until both the numbers entered are in schoolnumber. If a number not in the list or a string is entered it will display the error message and start over.

Okay the code should look like this (I don't know what sport it is, hope I didn't make mistakes)
MAX_SCHOOLS = 4
MAX_GAMES = 5
teams = [0]*MAX_SCHOOLS
for i in range(MAX_GAMES):
host = int(input("Who hosted the game [1 to {}]? ".format(MAX_SCHOOLS)))
winner = int(input("Who was the winner (0 for tie) ? "))
away = int(input("Away team [1 to {}] ? ".format(MAX_SCHOOLS)))
if winner == away:
print("Error : Away team = Host team")
continue # skip this game
if not 0 <= winner < MAX_SCHOOLS:
print("Error : winner incorrect")
continue
if not 0 <= host < MAX_SCHOOLS:
print("Error : host incorrect")
continue
if not 0 <= away < MAX_SCHOOLS:
print("Error : winner incorrect")
continue
if host == winner:
teams[host-1] += 2
elif away == winner:
teams[away-1] += 3
else:
teams[away-1] += 1
teams[host-1] += 1
print(teams)

Related

when i compare the arr array and guessed array why does nothing print? [closed]

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 4 days ago.
Improve this question
i am trying to compare two arrays to check for similarities, however nothing seems to be printing other than an empty array but i want to print an array with every similar item, this is my code.
import replit
import time
import random
import colored
from colored import fg, bg, attr
reset = attr(0)
bold = attr(1)
arr=[]
guessed=[]
correct = []
num = 0
stop = False
print(bold + "Welcome to Memory Game" + reset)
time.sleep(2)
difficulty = input('enter difficulty, easy, medium or hard: ')
if difficulty == 'easy':
num = 5493
timer = 7
if difficulty == 'medium':
num = 7
timer = 6
if difficulty == 'hard':
num = 10
timer = 6
print('you are playing',difficulty,'you have only',timer,'seconds to remeber as many numbers as possible')
time.sleep(3)
replit.clear()
score = 0
for x in range(num):
x = random.randint(1,9)
arr.append(x)
print(arr)
print(*arr, sep = ", ")
time.sleep(timer)
replit.clear()
guess = input('enter as many numbers as you remeber in order')
for x in guess:
guessed.append(x)
print(guessed)
while len(guessed) != len(arr):
guessed.append('_')
for i in range(len(arr)):
if guessed[i] == arr[i]:
correct.append(i)
print(correct)

Beginner question regard basic loop and totaling positive inputs [closed]

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 days ago.
Improve this question
print("Welcome to the number program")
number=input("Please give me a number \n")
number=int(number)
total_number=0
entries=0
while number>0:
total_number=total_number+number
print(total_number)
number=input("Please give me another number! \n")
number=int(number)
entries= int(entries)+1
if number < 0 :
print("Sorry, this value needs to be positive. Please enter a
different number.")
if number == -999:
print(total_number)
print(entries)
print(total_number/entries)
I'm in a beginners programming class, and the book is not very helpful at times. I'm trying to write a basic program that takes positive numbers, totals them, and averages them out at the end. Also rejects negative numbers, and asks if -999 is entered I print the average of all entries, amount of entries, and the value tally. Any advice or tips I can learn from to improve it would be helpful. Thanks!
The program runs ok, it just doesn't write out some things I wanted
From what you wrote and the comments in your code I am guessing that you want the program to continue running and asking for input if you enter a non-positive number. In that case I would rewrite it as:
print("Welcome to the number program")
total_number = 0
entries = 0
while True:
number = input("Please give me a number \n")
number = int(number)
if number == -999:
break
if number <= 0:
print("Sorry, this value needs to be positive. Please enter a different number.")
continue
total_number = total_number + number
entries += 1
print(total_number)
print(total_number)
print(entries)
print(total_number / entries)
Also, you can increment numbers with entries += 1
In most cases you should NOT create variables first, however this case you should. Create number = 0, tally = 0 and total_number = 0 first
Accept your first number inside your while loop and handle all of the logic in there as well.
Your while loop should continue to loop until the final condition is met which seems to be number == -999
Should tally be incremented if you enter a negative number? I assume not. What about a 0? Wrap the increment for tally and the addition to total_number in an if number > -1: condition. Use an if else to check for number == -999, and an else for handling invalid entries.
Finally, move your print statements outside of your while loop. It also doesn't need a condition around it because now, if you've exited your while loop, that condition has been satisfied.
Final note here, and this is just a nice to know/have and purely syntactic sugar, MOST languages support abbreviated incrementing. Theres a better word for it, but the gist is simply this.
total_number += number
# is exactly the same as
total_number = total_number + number
# but way nicer to read and write :)
print("Welcome to the number program")
number = 0
total_number = 0
entries = 0
while number != -999:
number = input("Please enter a number! \n")
number = int(number)
if number >= 0
total_number += number
entries += 1
print("Current sum: " + total_number)
elif number == -999:
break
else
print("Sorry, this value needs to be positive.")
print("Sum of entries: "+str(total_number))
print("Number of entries: " + str(entries))
print("Average entry: " +str(total_number/entries))
I have rewritten your code. But I am not sure what the goal was. Anyways, if the value were ever to be under 0 the loop would have been exited and a new value would have never been accepted from an input.
Also some things I have written more elegant.
print("Welcome to the number program")
number=int(input("Please give me a number \n"))
total_number=0
entries=0
while number > 0:
total_number += number
print(total_number)
number = int(input("Please give me another number! \n"))
entries += 1
if number == -999:
print(total_number)
print(entries)
print(total_number/entries)
break
elif number < 0:
number = input("Sorry, this value needs to be positive. Please enter a different number!")

Receiving integers from the user until they enter 0 [closed]

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 3 years ago.
Improve this question
It's my 2nd week in programming in Python and have never programmed anything before. appreciate step by step.
I don't know where to start.
try:
count = 0
while True:
user = int(input('Insert Number: '))
count += user
if user == 0:
break
print(count)
except ValueError:
print('Please Insert Numbers Only!')
Here a start, use a while loop for the input. I'll leave the summation part for you unless you need further help:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
while cc != 0:
cc = int(input("Enter an integer to sum. Press 0 to to end and start the summation:"))
def is_int(num):
try:
return int(num)
except:
return False
total = 0
while True:
in_user = is_int(input("input integer: "))
if in_user is not False:
total += in_user
if in_user is 0:
break
print(total)

How can i improve this? [closed]

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 4 years ago.
Improve this question
I am currently self teaching myself Python , and this is my first program. I am doing some simple projects so i can improve. Would like to hear feedback on this one.
import random
print("There is a number between 1 and 100 guess it and you will win a price : ")
def guess():
# A simple guess game.
random_number = random.randint(1, 5)
flag = True
user_guess = []
while flag:
user_number = input("Enter the number : ")
user_guess.append(user_number)
if int(user_number) < random_number:
print("You have to go higher")
elif int(user_number) > random_number:
print("You have to go lower ")
else:
flag = False
print("Congrats you win !!! \nThe numbers of guesses before you won : ")
print(len(user_guess))
guess()
while True:
play_again = input("Do you like to play again ? : (y/n): ")
if play_again == 'y':
guess()
else:
print("Thank you for playing have a nice day!!! ")
break
Your program will throw an exception if the user enters an invalid (nondigit) character. Use isdigit to avoid such cases.
Converting user_number to integer every time you want to compare it with some other integer is costly, and frivolous. Convert it once and reassign it to user_number.
flag is not necessary. Use while True, and break when needed.
Keeping an entire list object just to count user input is a bit overkill. Have a single integer (count in my example) and use it.
You print "between 1 and 100" but your program creates a random integer between 1 and 5. I am not sure about your intentions with doing that (perhaps a debugging aid), but I presume it's a small mistake.
A minor suggestion, move the print("There is a number between 1 and 100 guess it and you will win a price : ") to the guess function since it makes more sense for the function to declare its own purpose.
Here's how I would make those changes to the guess function:
def guess():
print("There is a number between 1 and 100 guess it and you will win a price : ")
# A simple guess game.
random_number = random.randint(1, 100)
# flag = True obsolete
count = 0
while True:
user_number = input("Enter the number : ")
if (user_number.isdigit()):
user_number = int(user_number)
else:
print("Invalid:")
continue
count += 1
if user_number < random_number:
print("You have to go higher")
elif user_number > random_number:
print("You have to go lower ")
else:
print("Congrats you win !!! \nThe numbers of guesses before you won : ")
print(count)
break

Python dice game [closed]

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 9 years ago.
Improve this question
I must create a dice game that generates numbers from 1 to 6. It will then throw the dice 50 times and it will count the number of odd numbers and even numbers. I'm using Python.
Here is my code:
import random
# Determine odd and even numbers
throws = 0
even = 0
odd = 0
maxthrows = 50
print "Even : Odd"
while True:
throws += 1
if throws == maxthrows:
break
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
print even, " : ", odd
raw_input("Press enter to exit.")
Your loop is wrong, it should be:
while throws != maxthrows:
throws += 1
dice = random.randrange(6)
if dice % 2 == 1:
odd += 1
else:
even += 1
Notice that:
Whenever possible, the exit condition should be used in the loop condition, not in an if ... break
The part where you ask if the dice is odd must be inside the loop, in Python indentation matters - a lot!

Categories