finding an odd digit in a number - python

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

Related

I'm trying to make a program that takes a number from the user and determine if this is a prime number by returning prime factors

I'm trying to make a program that takes a number from the user and determine if this is a prime number by returning prime factors
i tried setting an empty array and then making a for loop and append evey number that can be divider by the user input but i always get an empty string in output
`
user_number = input('choose your number: ')
number_dividers = []
for x in range(1,int(user_number)) :
if x % int(user_number) == 0 and x != int(user_number) :
number_dividers.append(x)
elif int(user_number) < 1 :
print('please enter a number higher than 0')
print(number_dividers)
`
To check if the number is prime, you will have to divide 'user_number' by x as opposite of what you've done in the code.
if int(user_number) % x == 0 and x != int(user_number) :
Also consider the final statements as -
elif int(user_number) < 2 :
print('please enter a number higher than 1')
This is because the conditions in your code will give 1 as a prime number. One is neither prime nor composite so the suggested case should avoid that error.

How to make simple number identification Python?

I have this exercise:
Receive 10 integers using input (one at a time).
Tells how many numbers are positive, negative and how many are equal to zero. Print the number of positive numbers on one line, negative numbers on the next, and zeros on the next.
That i need to solve it with control/repetition structures and without lists. And i'm stuck in the first part in dealing with the loops
Until now I only writed the part that deal with the amount of zeros, I'm stuck here:
n = float(input()) #my input
#Amounts of:
pos = 0 # positive numbers
neg = 0 # negative numbers
zero = 0 # zero numbers
while (n<0):
resto = (n % 2)
if (n == 0): #to determine amount of zeros
zz = zero+1
print (zz)
elif (resto == 0): #to determine amout of positive numbers
pp = pos+1
print (pp)
elif (n<0): #to determine amount of negative numbers
nn = neg+1
else:
("finished")
My inputs are very random but there are like negatives and a bunch of zeros too and obviously some positive ones. What specific condition i write inside while to make it work and to make a loop passing by all the numbers inside a range of negative and positive ones?
Soo.. i made it turn into float because theres some broken numbers like 2.5 and the inputs are separated by space, individual inputs of numbers one after other
example input (one individual input at a time):
25
2.1
-19
5
0
# ------------------------------------------
# the correct awnser for the input would be:
3 #(amount of Positive numbers)
1 #(amount of Negatives numbers)
1 #(amount of Zeros numbers)
how to make them all pass by my filters and count each specific type of it?
obs: i can't use lists!
If I understood you right, I think the below code may be what you are after.
Wishing you much success in your future ventures and projects!:D
pos_count = 0
neg_count = 0
zero_count = 0
turn_count = 0
while turn_count < 10:
user_input = input("Please enter a whole number: ") #Get input from user
try:
user_number = int(user_input) # Catch user input error
except:
print("Input not valid. Please enter a whole number using digits 0-9")
continue # Start over
# Check each number:
if user_number > 0:
pos_count += 1
turn_count +=1
elif user_number < 0:
neg_count += 1
turn_count +=1
elif user_number == 0:
zero_count += 1
turn_count +=1
print("Positive count:", pos_count) # Print positive count
print("Negative count:", neg_count) # Print negative count
print("Zero count:", zero_count) # Print zero count
Why not something like this?
pos = 0
neg = 0
zer = 0
for x in range(10):
number = int(input())
if number > 0:
pos +=1
if number < 0:
neg +=1
else: # number is not positive and not negative, hence zero
zer +=1
print(pos)
print(neg)
print(zer)
EDIT: Thanks #Daniel Hao for pointing out that casting to int is necessary with input().

Guess 4 digit combination game

I am trying a similar thing like this: 4 Digit Guessing Game Python . With little changes.
The program generates random numbers between 999 and 10000.User after every failed attempt gets how many numbers he guess in the right spot and how many numbers he got right but didn't guess position correctly.
Etc. a random number is 3691 and the user guess is 3619. He gets 2 numbers in the correct position (3 and 6) and also 2 numbers correct but in the wrong position (1 and 9).
There is no output when for numbers he didn't guess and guessing is repeating until all 4 digits are guessed in the right spot.
My idea is we save digits of the random number to a list and then do the same thing with user guess number. Then we compare the first item of both lists etc. combination_list[0] == guess_ist[0] and if it's correct we add +1 on counter we call correct.
The problem is I don't have an idea for numbers that are guessed correctly but are not in the correct position.
import random
combination = random.randint(1000, 9999)
print(combination)
digits_combination, digits_guess= [], []
temp = combination
while temp > 0:
digits_combination.append(temp % 10)
temp //= 10
digits_combination.reverse()
print(digits_combination)
guess= int(input("Your numbers are? "))
while not 999 < pokusaj < 10000:
pokusaj = int(input("Your numbers are? "))
if guess!= combination:
while guess> 0:
digits_guess.append(guess% 10)
guess//= 10
digits_guess.reverse()
if guess == combination:
print("Your combination is correct.")
correct_position= 0
correct= 0
test = digits_combination[:] # I copied the list here
while guess!= combination:
while guess> 0:
digits_guess.append(guess% 10)
guess //= 10
digits_guess.reverse()
if digits_guess[0] == test[0]:
correct_position += 1
I have this solution. I suggestion you to cast to string and after cast to list to get a list of number digits instead to use a while loop. For the question you can try to use "in" keywords to check if number in digits_combination but not in right position.
import random
combination = random.randint(1000, 9999)
print(combination)
digits_combination = list(str(combination))
guess= int(input("Your numbers are? "))
while not 999 < guess < 10000:
guess = int(input("Your numbers are? "))
digits_guess = list(str(guess))
if guess == combination:
print("Your combination is correct.")
correct_position = 0
correct = 0
for index, value in enumerate(digits_guess):
if value == digits_combination[index]:
correct_position += 1
elif value in digits_combination:
correct += 1

how to find unique letters in string

I have to create a program that generates a five digit number which a user has to guess by getting different clues like how many digits they have correct and how many are in the correct position.
The function i have written out now it to find the unique letters aka the letters that each string has in common. Now this works if the length is exactly 5 letters. But i need to have a statement written out (this is too short or long) when the user exceeds a length of 5 or is lower than 5. It says this but counts what is right and adds it to the previous number. This shouldnt be there. Also the numbers shouldnt add only state the right amount in that attempt. Heres it visually:
rannum remove : 24510
enter number: 24511
4
enter number: 12
this is too short
6
heres the code:
while not userguess:
guess = str(input("enter number: "))
if len(guess) < 5:
print("this is too short")
for i in list(set(secretString) & set(guess)):
uniquedigits_found += 1
print(uniquedigits_found)
is there anyway to fix this problem?
You should try resetting your unique digits variable in each iteration of the while loop, and separate the for loop to check matching digits in an else statement:
while not userguess:
uniquedigits_found = 0
guess = str(input("enter number nerd: "))
if len(guess) < 5:
print("this is too short")
elif len(guess) > 5:
print("this is too long")
else:
for i in list(set(secretString) & set(guess)):
uniquedigits_found += 1
print(uniquedigits_found)

How to break out of continue statement after certain amount of tries?

Apologies if the question to this is worded a bit poorly, but I can't seem to find help on this exercise anywhere. I am writing a basic Python script which sums two numbers together, but if both numbers inputted are the same the sum will not be calculated.
while True:
print('Please enter a number ')
num1 = input()
print('Please enter a second number ')
num2 = input()
if num1 == num2:
print('Bingo equal numbers!')
continue
elif num1 == num2:
print('It was fun calculating for you!')
break
print('The sum of both numbers is = ' + str(int(num1) + int(num2)))
break
If both numbers are equal I want the script to loop back once more and if the numbers inputted are equal again I want the program to end. With the code I have provided the issue I am having is that when I enter two equal numbers it keeps constantly looping until I enter two different numbers.
You would likely want to have a variable keeping track of the number of times that the numbers were matching. Then do something if that counter (keeping track of the matching) is over a certain threshold. Try something like
matches = 0
while True:
num1 = input('Please enter a number: ')
num2 = input('Please enter a second number: ')
if num1 == num2 and matches < 1:
matches += 1
print('Bingo equal numbers!')
continue
elif num1 == num2:
print('It was fun calculating for you!')
break
print('The sum of both numbers is = ' + str(int(num1) + int(num2)))
break
You can add give input code again inside first if statement or use some other dummy variable for loop so that you can break the loop, for e.g. use while j == 0 and increase it j += 1when you are inside the first if statement
continue skips the execution of everything else in the loop. I don't see it much useful in your example. If you want to print the sum then just remove it.
How continue works can be demonstrated by this sample (taken from python docs)
for num in range(2, 10):
if num % 2 == 0:
print("Found an even number", num)
continue
print("Found a number", num)
Result
Found an even number 2
Found a number 3
Found an even number 4
Found a number 5
Found an even number 6
Found a number 7
Found an even number 8
Found a number 9

Categories

Resources