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!
Related
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 3 months ago.
Improve this question
I wrote this code snippet:
lowestNumber = int(input("\nWhat would you like your lowest number to be?"))
highestNumber = int(input("What would you like your highest number to be?"))
number = random.randint(lowestNumber, highestNumber)
tries = 0
while tries < 10:
guess = int(input(f'\nEnter a number between', lowestNumber))
if guess == number:
print("You guessed correctly! The number was", number)
break
elif guess < number:
print("Too low!")
elif guess > number:
print("Too high!")
tries += 1
SyntaxError: bad input on line 22 in main.py.
Line 22 was guess = int(input(f'\nEnter a number between', lowestNumber)).
I searched it up on google and got nothing, I pasted it into OpenAI's code fixing and it also didn't help.
How can I fix this error?
When you wrote
guess = int(input(f'\nEnter a number between', lowestNumber))
it passed both the string and lowestNumber into the input function. However, you probably wanted to write something like Enter a number between (lowestNumber) and (highestNumber). To do this, you would have to write
guess = int(input(f'\nEnter a number between {lowestNumber} and {highestNumber}. '))
In my example, it passes in one object, the string, which contains lowestNumber and highestNumber in it. In your example, it passes in two objects, the string and lowestNumber.
The formatting you did in the input functions works in print statements, so the print statements are correct, but the input function is not.
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 1 year ago.
Improve this question
#WAP to check given number is Armstrong or not, (done)
#if it is Armstrong then print reverse of that number, (done)
#if it is not Armstrong then check it is Palindrome or not. (problem)
no=int(input("Enter your number:"))
temp=no
arm=0
rev=0
while(no>0):
rem=no%10
cube=rem*rem*rem
arm=arm+cube
no=no//10
if(temp==arm):
while (temp> 0):
rem = temp % 10
rev = (rev * 10) + rem
temp = temp // 10
print("Reverse is:", rev)
elif(temp!=arm):
while (temp > 0):
rem = temp % 10
rev = rev * 10 + rem
temp = temp // 10
if(rev==temp):
print("It's a palindrome.")
else:
print("It's not a palindrome.")
I can't find out the problem with the "check if it is a palindrome" part.
In your code to check for palindrome, you are repeatedly dividing your temp value by 10, but your condition is for temp>0 which will never be reached, as repeated division by 10 will never result in a negative number. So, you should change your condition to while(temp>=1).
Also, you should compare the final value of rev to no instead of with temp.
So if you change your final condition to if(rev==no): it should work. This is because your temp keeps getting modified in your loop to check for palindrome, whereas you want to compare your final rev with the original number.
Try this to check if number is palindrome or not
if str(temp)==str(temp)[::-1]:
print("Number is palindrome")
else:
print("Not palindrome")
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 2 years ago.
Improve this question
I want a program that sums the even numbers of a bigger number using while function.
Exemple :
Number is : 12345
Print : 6 (2+4)
This is what i wrote so far:
num = int(input("Introduce a non negative number: "))
if num % 2 == 0:
sum += num
print("sum")
I can't stress this enough
When doing school assignments, the whole idea with assignments is to teach you concepts, not solutions. There's a reason why you were given this assignment, asking others to solve it for you - is not the way to go.
Go back to your teacher, and ask for help if something is unclear. But because others start posting solutions I might as well just keep mine here.
Skipping the conversion to an early integer, will allow you to iterate over it as a string, and grab one number a a time.
num = input("Introduce a non negative number: ")
total = 0
for i in num:
if int(i) % 2 == 0:
total += int(i)
print("sum:", total)
You can then use your original logic, with some minor modifications.
Since for whatever reason, you're only allowed to use while and not for, you'd have to just adapt a bit.
num = input("Introduce a non negative number: ")
total = 0
i = 0
while i < len(num):
if int(num[i]) % 2 == 0:
total += int(num[i])
i += 1
print("sum:", total)
While I'm at it, after reading my code again. I am quite sure that a while loop here is the least pretty solution to this problem. But from a teaching standpoint there might be some benefit here. But I'd recommend going with the for loop if at all possible.
Try this one:
# split the number to list of digits
digits_list = list(input("Introduce a non negative number: "))
total = 0
# loop over the digits list and pick a digit till the list is empty
while digits_list:
digit = digits_list.pop()
if int(digit) % 2 == 0:
total += int(digit)
print(total)
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)
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)