how to find unique letters in string - python

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)

Related

how i can make odd and even number with pyrhon? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 14 days ago.
i'm new in python
and for practice i make this codes but i have a problem :when you choose a number more than 100 it says you entered a wrong number and you must enter another number then if you enter a right number you wont get the awnser
this is outout
please enter a number between 0 and 100 =>123
you have entered a number more than 100 or less than 0 !
so please enter a number between 0 and 100 =>12
and nothing !!!
but if you enter a wrong number for two times or more it will work perfectly
this is my code
print("welcome to or simple test")
def number_choosing_1():
number_1=int(input("please enter a number between 0 and 100 "))
if 0<number_1 and number_1<100 and number_1%2==0:
print("the number you have entered is even ")
elif 0<number_1 and number_1<100 and number_1%2==1:
print("you have entered a odd number ")
else :
if number_1>100 or number_1<0:
wrong_number_choosing_1()
elif 0<number_1 and number_1<100:
number_choosing_1()
def number_choosing_2():
number_1=int(input("that's it now fore make me sure reenter your number "))
if 0<number_1 and number_1<100 and number_1%2==0:
print("the number you have entered is even ")
elif 0<number_1 and number_1<100 and number_1%2==1:
print("you have entered a odd number ")
else :
if number_1>100 or number_1<0:
wrong_number_choosing_1()
elif 0<number_1 and number_1<100:
number_choosing_1()
def wrong_number_choosing_1():
number_1=int(input("""you have entered a number more than 100 or less than 0 !
so please enter a number between 0 and 100 """))
while number_1>100 or number_1<0:
number_1=int(input(" come on again !! please enter a number between 0 and 100 "))
if 0<number_1 and number_1<100:
number_choosing_2()
number_choosing_1()
any help appreciated .
just use a loop to ask for an input until a valid answer is given, then break the loop
while True:
number = int(input("Enter a number between 0 and 100: "))
if 0 <= number <= 100:
break
else:
print("Wrong number, try again")
# then check if the number is even or odd
parity = "odd" if number % 2 else "even"
print(f"The number {number} is {parity}")

Problem I am facing using the len command and while loop

So I have this block of code, which is supposed to figure out how many letters there are in the input. If it is greater than 1 or less than 1(no input), there is an error. However, when running this code, it still prints out "You entered an invalid..." even when I only input in a single letter, which shouldn't be passing through the while loop because its length is only 1. Idk why this is happening any beginner friendly help is appreciated!
letter_guess = input("Enter a single letter to guess: ")
length = len(letter_guess)
while length > 1 or length < 1:
letter_guess = input("You entered an invalid amount of letters, please guess again: ")
You should add length = len(letter_guess) in while loop after the input. As it's not updating currently.
You just need to update the length variable again when you ask for the next guess.
letter_guess = input("Enter a single letter to guess: ")
length = len(letter_guess)
while length > 1 or length < 1:
letter_guess = input("You entered an invalid amount of letters, please guess again: ")
length = len(letter_guess)

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

what is causing reference before assignment errors in below code?

I'm getting this error with refrenced before assignment and im not sure how to fix it.
I havent tried anything at the moment. It would be appreciated if this could be answered. (im just trying to fill up more words so it can be posted)
this is the error code i am getting:
number = int(number)
UnboundLocalError: local variable 'number' referenced before assignment
And this is the rest of my code
import random
import sys
again = True
while True:
myName = input('Hello, Enter your name to get started')
if myName.isdigit():
print('ERROR,Your Name is not a number, please try again')
print('')
continue
break
myName = str(myName.capitalize())
print('')
print('Hi {}, This is Guessing Game, a game where you have a certain amount of attempts to guess a randomly generated number. Each level has a different amount of attempts and a higher range of number. After each guess, press enter and the program will determine if your guess is correct or incorrect.' .format (myName))
print('--------------------------------------------------------------------------------')
while True:
level = input('{}, Please select a level between 1 and 3. Level 1 being the easiest and 3 being the hardest')
if not level.isdigit():
print('Please enter a number between 1 and 3. Do not enter a number in word form')
continue
break
def guessNumber(): # Tells the program where to restart if the user wants to play again
guessesTaken = 0
List = []
if level == 1:
number = random.randint (1, 16)
print('You chose Level 1, Guess a number a between 1 and 16, you have 6 guesses.')
allowedGuesses = 6
boundary = 16
if level == 2: # The code for level 2
number = random.randint (1,32)
print('You chose Level 2, Guess a number between 1 and 32, You have 8 guesses.')
allowedGuesses = 8
boundary = 32
if level == 3:
number = random.randint (1, 40)
print('You chose Level 3, Guess a number between 1 and 40, you have 10 guesses.')
allowedGuesses = 10
boundary = 40
if level == 4:
number = random.randint (1, 50)
print('You chose Level 4, Guess a number between 1 and 50, you have 10 guesses.')
allowedGuesses = 10
boundary = 50
if level == 5:
number = random.randint (1, 60)
print('You chose Level 5, Guess a number between 1 and 60, you have 10 guesses.')
allowedGuesses = 10
boundary = 60
guess = input()
guess = int(guess)
while guessesTaken < allowedGuesses:
guessesTaken = guessesTaken + 1
guessesLeft = allowedGuesses - guessesTaken
if guess < number:
List.append(guess)
print('Your guess is too low, You must guess a higher number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))
if guess > number:
List.append(guess)
print('Your guess is too high, You must guess a lower number, you have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))
if guess > boundary:
List.append(guess)
print('You must input a number between 1 and 16. You have {} guesses remaining. You have guessed the numbers {}, Take another guess' .format (guessesLeft, List))
if guess == number:
List.append(guess)
print('Good Job {}!, You guessed my number in {} guesses. You guessed the numbers {}.' .format (myName, guessesTaken, List))
print('Your high score for your previous game was {}' .format(guessesTaken))
else:
number = int(number)
print('')
print('--------------------------------------------------------------------------------')
print('Sorry {}, Your gueses were incorrect, The number I was thinking of was {}. You guessed the numbers {}.' .format(myName, number, List))
guessNumber()
print('')
print('It is recommended to pick a harder level if you chose to progress')
print('')
while True:
again = input('If you want to play again press 1, if you want to stop playing press 2')
if not again.isdigit():
print('ERROR: Please enter a number that is 1 or 2. Do not enter the number in word form')
continue
break
if again == 1:
level + 1
guessNumber()
if again == 2:
print('Thanks for playing Guessing Game :)')
sys.exit(0)
In your code you are getting level as input and checking that if level is in between 1 to 5.
else you are trying number = int(number)
but you should write number = int(level).
Since level is a string rather than a number, none of the conditions like
if level == 1:
will succeed. So none of the assignments like number = random.randint (1, 16) ever execute, and number is never assigned.
Since if level == 5: doesn't succeed, it goes into the else: block, which starts with
number = int(number)
Since none of the other number assignments took place, this tries to use int(number) before the variable has been assigned, which doesn't work.
I'm not sure why you even have this assignment there. When number is assigned, it's always set to an integer, so there's no need to use int(number).
You need to use
level = int(level)`
after you confirm that it contains digits. And you need to do similarly with again.
There are a number of other problems with your code. For instance, the code that asks for the user's guess and checks it is inside the if level == 5: block, it should run in all the levels.
When you have a series of mutually exclusive tests, you should use elif for each successive test. If you just use if for each of them, and then use else: at the end, that else: will only apply to the last test, so it will be executed even if one of the early tests also succeeded.

finding an odd digit in a number

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

Categories

Resources