This is for a school assignment and I'm incredibly new to programming in general, the purpose of the code is to create a little dialog as if the user were speaking with Count Von Count. In this section of the assignment we have to take the users favorite number and a chosen number and if they aren't divisible by each other than we count by the users favorite number to the next highest number from the chosen number that is divisible. Sorry if I explained it in some strange way that no one really understands, I'm not the best with explaining.
count = 0
favorite_number = input("What is your favorite number")
chosen_number = input("Choose a number")
divisibility = int(chosen_number) % int(favorite_number)
if int(chosen_number) % int(favorite_number) != 0:
while int(divisibility) != 0:
int(chosen_number) + 1
if int(divisibility) == 0:
print(f"The Count: {chosen_number} isn't divisble by {favorite_number} so lets try counting to {divisibility} instead! I will be counting by {favorite_number} to {divisibility}")
while (count) < int(divisibility):
print((count)-2)
(count) += int(favorite_number)
if (count) < int(divisibility):
print(f"The Count: And thats how you count to {divisibility}!")
Related
import random
money=int(0) # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
number = int(input("Pick a number 0-30"))
if number % 2 == 0: #checks if number is divisible by 2
print("The number was even, you get 2x your money.") #prints the users winnings
money = float(bet) * float(2) # multiplies the bet by 2
else:
money = bet+0 # doesnt add anyuthing to the bet
if number % 10 ==0: #checks if number is divisible by 10
print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
money = bet*3 # multiplies the bet by 3
else:
money = bet+0 # doesnt add anyuthing to the bet
primenum = ["3","5","7","11","13","17","19","23","29"] #list of prime numbers
if number == primenum ==0: #if number chosen by user = one of the numbers on the list, the user wins.
print("The number was a prime number, you get 5x your money.") #prints the users winnings
money = bet*5 # multiplies the bet by 5
else:
money=bet+0 # doesnt add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round
Why doesn't this code work? I have tried multiple different ways yet the bet doesn't seem to multiply.
I was expecting the code number to be multiplied at the end.
You have three different if-else blocks. All three do different things to money if their respective conditions evaluate to True. However, if the last if-else block evaluates False, then regardless of what happened in the previous if-else blocks, the code in the final else block will run. That code sets money back to the original bet.
You can see this happening if you introduce print (money) after each if-else block and re-run your code.
Also - it's not clear to me how number == primenum == 0 would ever evaluate to True. primenum is a list of strings. number is an int. Therefore, this condition will never evaluate to True.
If what you're trying to do is check whether number is prime, then I would (first), define primenum as a list of integers, like so:
primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers
and then, change the criterion in your last if-else block to:
if number in primenum:
One way to make your if-else blocks work would be to use if-elif-else, like so:
import random
money=int(0) # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers
number = int(input("Pick a number 0-30"))
if number % 10 == 0:
print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
money = float(bet) * float(2)
elif number % 2 == 0:
print("The number was even, you get 2x your money.") #prints the users winnings
money = bet*3
if number in primenum: #if number chosen by user = one of the numbers on the list, the user wins.
print("The number was a prime number, you get 5x your money.") #prints the users winnings
money = bet*5 # multiplies the bet by 5
else:
money=bet+0 # doesnt add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round
Bear in mind that "winningnumber" hasn't played a role in the program at all - except to be generated at the start, and printed to screen at the end. The above may not reflect your original intent, it's just ONE way in which the control-flow could be made to work so you reliably modify money when any of the three conditions evaluates to True.
The problem with your code relates to the multiple if-else blocks.
You have a problem with your prime number section, which will always equate to False, and as such you will set money=bet+0 - which will override any other successful changes made to money.
Your winning number does not seem to have any impact on the flow of code, so as long as you choose an even number or a number divisible by 10, it should multiply. But then when you get to the section to test if it is a prime number, it will ALWAYS equate to FALSE, and as mentioned before will set money=bet+0 - and undo anything done prior.
You need to make sure that you set money = money + (bet + 0)
This way your cumulative effects are maintained. You have to do this for the other money calculations as well.
And you need to fix the prime number section:
primenum = [3,5,7,11,13,17,19,23,29]
if number in primenum:
...
Here is the complete code based on what I think you were trying to do:
import random
money=0 # money starts at 0 always
bet=int(input("enter a bet.")) # user inputs a bet.
winningnumber=(random.randint(0,30)) # chooses random integer from 0 - 30, stores it as variable winningnumber
number = int(input("Pick a number 0-30"))
primenum = [3,5,7,11,13,17,19,23,29] #list of prime numbers
if winningnumber == number:
if number % 2 == 0: #checks if number is divisible by 2
print("The number was even, you get 2x your money.") #prints the users winnings
money = money + (bet * 2) # multiplies the bet by 2
else:
money = money + (bet + 0) # doesn't add anything to the bet
if number % 10 == 0: #checks if number is divisible by 10
print("The number was a multiple of 10, you get 3x your money.") #prints the users winnings
money = money + (bet * 3) # multiplies the bet by 3
else:
money = money + (bet + 0) # doesn't add anything to the bet
if number in primenum: #if number chosen by user = one of the numbers on the list, the user wins.
print("The number was a prime number, you get 5x your money.") #prints the users winnings
money = money + (bet * 5) # multiplies the bet by 5
else:
money = money + (bet + 0) # doesn't add anything to the bet
print("the winning number was",winningnumber) #shows the player the winning number
print("Your money for the end of the round is",money) #prints money at end of round
I'm trying to solve this problem below. I can get it to print whether it's odd or even but I can't get it to print out the correct message if number is a multiple of 4.
Here is the problem: Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. If the number is a multiple of 4, print out a different message.
Here is my code:
number = input("Pick a number and I'll tell you if it's odd or even. ")
def odd_or_even():
if int(number) % 2 == 0:
return("Your number is even.")
elif int(number) % 4 == 0:
return("Your number is a multiple of 4.")
else:
return("Your number is odd.")
print(odd_or_even())
If a number is a multiple of 4, it is also an even number and that's why it always triggers your first condition and doesn't even check the second one. Change the order of the conditions, i.e.:
...
if int(number) % 4 == 0:
return("Your number is a multiple of 4.")
elif int(number) % 2 == 0:
return("Your number is even.")
...
import random
#Making sure all values are = to 0
one = 0
two = 0
three = 0
four = 0
five = 0
six = 0
#Loop for rolling the die 100 times
for r in range(0, 100):
roll = random.randint(1, 6)
if roll == 1:
one += 1
elif roll == 2:
two += 1
elif roll == 3:
three += 1
elif roll == 4:
four += 1
elif roll == 5:
five += 1
elif roll == 6:
six += 1
#print how many times each number was rolled
print(one)
print(two)
print(three)
print(four)
print(five)
print(six)
#How many times the 3 was rolled
print("The 3 was rolled", three, "times!")
#Average roll between all of them
print("The average roll was", (one * 1 + two * 2 + three * 3 + 4 * four + 5 * five + 6 * six)/100)
I am trying to make it so it prints out
"number is the most common roll." for whichever the roll is.
Just trying to do this is the most simple way, and I'm confused on how to do it. I tried to do like if one > two > three etc etc. but that did not work.
Choosing an appropriate data structure to gain maximum leverage of a language’s core features is one of the most valuable skills a programmer can develop.
For this particular use case, it’s best to use an iterable data type that facilitates operations (e.g. sort, obtain the maximum) on the collection of numbers. As we want to associate a number (1-6) with the number of times that number was rolled, a dictionary seems like the simplest data structure to choose.
I’ve re-written the program to show how its existing functionality could be re-implemented using a dictionary as its data structure. With the comments, the code should be fairly self-explanatory.
The tricky part is what this question is actually asking: determining the number rolled most often. Instead of manually implementing a sorting algorithm, we can use the Python built-in max function. It can accept an optional key argument which specifies the function that should be applied to each item in the iterable object before carrying out the comparison. In this case, I chose the dict.get() method which returns the value corresponding to a key. This is how the dictionary of roll results is compared by the number of rolls for each result for determining the number rolled most often. Note that max() only returns one item so in the case of a tie, only one of the results will be printed (see Which maximum does Python pick in the case of a tie?).
See also: How do I sort a dictionary by value?
import random
NUM_ROLLS = 100
DIE_SIDES = 6
# Create the dictionary to store the results of each roll of the die.
rolls = {}
#Loop for rolling the die NUM_ROLLS times
for r in range(NUM_ROLLS):
roll_result = random.randint(1, DIE_SIDES)
if roll_result in rolls:
# Add to the count for this number.
rolls[roll_result] += 1
else:
# Record the first roll result for this number.
rolls[roll_result] = 1
# Print how many times each number was rolled
for roll_result in range(1, 7):
print("The number", str(roll_result), "was rolled", str(rolls[roll_result]), "times.")
#How many times the 3 was rolled
print("The number three was rolled", str(rolls[3]), "times.")
#Average roll between all of them
sum = 0
for roll_result in rolls:
sum += roll_result * rolls[roll_result]
print("The average roll result was", str(sum/NUM_ROLLS))
# The number rolled most often.
print(str(max(rolls, key=rolls.get)), "is the most common roll result.")
I am relatively new to programming with python (actually programming in general). I am making this 'Guess My Age' program that only has one problem:
import random
import time
import sys
print("\tAge Guesser!")
print("\t8 tries only!")
name = input("\nWhat's your name? ")
num = 80
min_num = 6
tries = 1
number = random.randint(min_num, num)
print("\nLet me guess... You are", number, "years old?")
guess = input("'Higher', 'Lower', or was it 'Correct'? ")
guess = guess.lower()
while guess != "correct":
if tries == 8:
print("\n I guess I couldn't guess your age....")
print("Closing...")
time.sleep(5)
sys.exit()
elif guess == "higher":
print("Let me think...")
min_num = number + 1 #### Here is my trouble - Don't know how to limit max number
time.sleep(3) # pause
elif guess == "lower":
print("Let me think...")
num = number - 1
time.sleep(3) # pause
number = random.randint(min_num, num) #<- Picks new random number
print("\nLet me guess... You are", number, "years old?")
guess = input("'Higher', 'Lower', or was it 'Correct'? ")
guess = guess.lower() #<- Lowercases
tries += 1 #<- Ups the tries by one
print("\nPfft. Knew it all along.")
time.sleep(10)
As you can see, I have 'num' as the max number for the random integer getting picked, but with:
elif guess == "higher":
print("Let me think...")
min_num = number + 1
it can go back up to however high it wants.
I want it to remember the last integer that 'num' was.
Say the program guessed 50 and I said 'Lower'. Then it said 30 and I said 'Higher'
I know I am probably sounding confusing, but please bear with me.
You need to define a maximum number as well as a minimum number. If they say their age is lower than a given age, you should set that age minus 1 as the maximum.
Of course, you also need to set an initial maximal age.
You might find it more useful to look into recursive functions for this kind of problem. If you define a function which takes min_age, max_age and tries_left as parameters, which comes up with a random number with between min_age and max_age and queries the user, you can then rerun the function (within itself) with a modified min_age, max_age and tries_left - 1. If tries_left is zero, concede defeat. This way you might get a better understanding of the logical flow.
I have left code out of this answer because, as you are a beginner, you will find it a useful exercise to implement yourself.
Cant you split out your guess into something like
max_num = 0
min_num = 0
elif guess =="lower":
max_num = number
if min_num!=0:
number = min_num+(max_num-min_num)/2
else:
number = max_num-1
elif guess =="higher":
min_num = number
if max_num!=0:
number=min_num+(max_num-min_num)/2
else:
number=min_num+1
Sorry it's not meant to be fully rigorous, and its a slight change on the logic you have there, but splitting out your variables so you have a higher and lower cap, that should help a lot?
Cheers
Please let me know if you need more elaboration, and I can try to write out a fully comprehensive version
It seems as though I was wrong in the fact that it did not remember the older integers. Before when running the program it would guess a number higher than the 'num' had specified. I don't know what I changed between then and now? But thank you for the help! #.#
This seems to work.
The only changes I really made:
-Variable names were confusing me, so I changed a couple.
-Note that if you try to mess with it (lower than 5, higher than 3... "Is it 4?" if you say it's higher or lower, you'll get an error).
The first time you set min and max numbers, you do it outside of the loop, so this script does "remember" the last guess and applies it to the new min, max inside of the loop. Each time it runs, the min will get higher or the max will get lower, based on the feedback from when the user checks the guess. If you had stuck the "min_num=6" and the "num=80" inside of the loop, the guesses would never get better.
import random
import time
import sys
print("\tAge Guesser!")
print("\t8 tries only!")
name = input("\nWhat's your name? ")
max_num = 10
min_num = 1
tries = 1
guess = random.randint(min_num, max_num)
print("\nLet me guess... You are", guess, "years old?")
check = raw_input("'Higher', 'Lower', or was it 'Correct'? ")
check = check.lower()
while check != "correct":
if tries == 8:
print("\n I guess I couldn't guess your age....")
print("Closing...")
time.sleep(5)
sys.exit()
elif check == "higher":
print("Let me think...")
min_num = guess + 1
time.sleep(3) # pause
elif check == "lower":
print("Let me think...")
max_num = guess - 1
time.sleep(3) # pause
guess = random.randint(min_num, max_num) # <- Picks new random number
print("\nLet me guess... You are", guess, "years old?")
check = input("'Higher', 'Lower', or was it 'Correct'? ")
check = check.lower() # <- Lowercases
tries += 1 # <- Ups the tries by one
print("\nPfft. Knew it all along.")
time.sleep(10)
So i have to make a program in python using a while loop. It goes like this: input an integer until it is 0.the program has to write out how many of inputed numbers has at least 1 odd digit in it.i don't know how to find odd digits in a number for which i don't know how many digits it has.i need this for school :/
As others have commented, the question you have asked is a little unclear. However, perhaps this is something like you are looking for?
odd_count = 0
user_number = None
# Ask for a user input, and check it is not equal to 0
while user_number != 0:
user_number = int(input("Enter and integer (0 to quit): "))
# Check for odd number by dividing by 2 and checking for a remainder
if user_number % 2 != 0:
odd_count += 1 # Add 1 to the odd number counter
print("There were {} odd numbers entered".format(odd_count))
number=int(input())
i=0
odd_number_count=0
while number>0:
for k in str(number):
if int(k)%2==0:
i=0
else:
i=i+1
if i>>0:
odd_number_count=odd_number_count+1
number=int(input())
print(odd_number_count)
this is how i solved it