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
Related
I made five chances for the guesser to guess the random number. Every time the guesser did wrong, I want to print:
Nice try, guess again (try bigger), you get {x} chance left.
The x is a countdown, so the first wrong guess will say you get 4 chances left, then another wrong, three chances left, and so on. How can I do that? Or, how to add a loop on the x.
So this is my unsuccessful code:
import random
secret_num = random.randint(1, 20)
print('Guess the number from 1-20 (you get 5 chances)')
for guess_chance in range(1, 6):
guess = int(input("Input guess number: "))
if guess < secret_num:
x = 5
x -= 1
print(f'Nice try, guess again (try bigger), you get {x} chance left.')
elif guess > secret_num:
x = 5
x -= 1
print(f'Nice try, guess again (try smaller), you get {x} chance left.')
else:
break
if guess == secret_num:
print('Congratz, you guess correctly in' + str(guess_chance) + 'times.')
else:
print('Nope, the correct number is ' + str(secret_num) + '.')
You need to do x = 5 before for guess_chance in range...) and remove it from inside the loop.
print('Guess the number from 1-20 (you get 5 chances)')
x = 5
for guess_chance in range(1, 6):
guess = int(input("Input guess number: "))
if guess < secret_num:
x -= 1
print(f'Nice try, guess again (try bigger), you get {x} chance left.')
elif guess > secret_num:
x -= 1
print(f'Nice try, guess again (try smaller), you get {x} chance left.')
else:
break
Otherwise, each guess you are resetting it to 5 tries and then subtracting one... which presumably always shows 4 tries...
There are many other ways to solve this... I just picked the one that changed your code the least
and when combined with your range this is a bit redundant
x = 6 - guess_chance would work
as would just iterating the range in reverse
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().
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)
random = random.randint(1000, 9999)
guess = int(input("Enter your guess: "))
while guess != random:
guess = int(input("That was incorrect! Enter your guess: "))
This works as a very simple guessing game, however I would like to include something where after every unsuccessful try it would say how many numbers out of the four digit number were correct.
I have not attempted this, mainly because I'm not sure how this could be done.
e.g
random = 1234
Enter your guess: 1111
You guessed 1 number correct
Enter your guess: 1222
You guessed 2 numbers correct
...... and so on
If you meant with positions
random = random.randint(1000, 9999)
guess = int(input("Enter your guess: "))
while guess != random:
right = 0
index = 0
for char in str(guess):
if char == str(random)[index]:
index += 1
right += 1
print(f'{right} were right')
guess = int(input("That was incorrect! Enter your guess: "))
If you really wanted to keep both numbers as int, you could use % (modulo operator) to find the remainder of your integer once divided by its base-10 value.
For example.
correctnum = 0
random = 1234
guess = 1111
if ((((random-(random % 1000 )) /1000) == (((guess-(guess % 1000 )) /1000)):
correctnum++
Formula repeated for 100/10/1 should all compare numbers in that "spot" without having to convert datatypes. Follow that up with an output of the value of correctnum and you should have what you need.
random = 1234
random = str(random)
#guess = str(input('enter your guess'))
guess = '1222'
correct = 0
for i in range(len(guess)):
if guess[i] == random[i]:
correct += 1
print(correct)
ouput: 2
or simply:
correct = sum(guess[i] == random[i] for i in range(len(guess)))
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