Can't get my program to continue properly until true [closed] - python

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 6 years ago.
Improve this question
Since you are not liking my explanation of my program I have changed it purely to only a question now:
how do I allow the program to continue to check what the input was and apply a rule according until the output is mu
x = input("Enter your string: ")
while not set(x).issubset({'m', 'u', 'i'}):
print("false")
x = input("Enter your string")
print("Your String is " + x)
if x == ("mu"):
print("game complete")
quit()
#I understand that right now I am only checking if x is mu and then
#displaying the question input
#not sure how to bring the rules into the loop too
else:
while x !=("mu"):
Question = int(input("Which rule would you like to apply? enter numbers 1-4: ")
if Question is 1:
x = (x + "l")
print(x)
elif Question is 2:
print("2")
elif Question is 3:
print("3")
elif Question is 4:
print("4")
elif Question is not 1 or 2 or 3 or 4:
print("invalid rule try again")

You bring the rules into the while-loop by indenting them accordingly:
while x != "mu":
Question = int(input("Which rule would you like to apply? enter numbers 1-4: ")
if Question == 1:
x = (x + "l")
print(x)
elif Question == 2:
print("2")
elif Question == 3:
print("3")
elif Question == 4:
print("4")
else:
print("invalid rule try again")
By the way: don't us is to compare numbers. Your last elif-condition is wrong, should be Question not in (1, 2, 3, 4) or even better a simple else.

Related

Is there any way to shorten it? [closed]

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 14 days ago.
Improve this question
n = [1,2,3,4,5,6]
while True:
num = (input("enter your choice(from 1 to 6) "))
if num not in str(n) or num.isdigit == "False":
print("enter valid chice ")
os.system('cls')
pr()
else:
break
I want it to loop if the input is string and not in n
n = [1,2,3,4,5,6]
while True:
num = input("enter your choice(from 1 to 6) ")
if not num.isdigit() or int(num) not in n:
print("enter a valid choice ")
else:
break
The issue in the original code was checking isdigit as a string instead of calling the method on num. Also, the condition to check if num is not in n was incorrect. The corrected code checks if num is not a digit and also checks if the integer form of num is not in n.
Your code is convoluted, here's a simpler version:
# use try/except to get an int, or set our var outside the bounds in the while loop
try:
num = int(input("Enter your number (1-6):"))
except ValueError:
num = 0
# while our input isn't in the 1-6 range, ask for a number
while num not in [1,2,3,4,5,6]:
# same as before, if this is not a number, just set it to something that's not in [1-6] so we stay in the while loop
try:
num = int(input("Enter a valid choice please (1-6):"))
except ValueError:
num = 0
# your code here

how to make if/elif/else more efficient [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
i'm stuck and need a little help.
how can i make this more efficient and reduce the number of if/elif/else.
i thought to make a function that check the range of an input let's say between 1 to 5 and then return the value to print out what i need.
i would love to hear your thoughts on it:
there some code:
while True:
difficulty = input("Please choose difficulty from 1 to 3: ")
if not difficulty.isdigit():
print("Please enter a valid number: ")
else:
break
while True:
if difficulty == "1":
print("Level of difficulty is very easy.")
break
elif difficulty == "2":
print("Level of difficulty is easy.")
break
elif difficulty == "3":
print("Level of difficulty is normal.")
break
else:
difficulty = input("You chose an invalid number, choose between 1 - 3. Try again:")
Ideally, you check the range of the number in the first loop
Other than that, use a list
descriptions = [
"very easy, ok you are scared to loose but let's play.",
"easy, ok let's play."
]
while True:
i = int(difficulty) - 1
if i not in range(5):
# invalid input, get it again
difficulty = input("must be between 1 and 5: ")
continue
lines()
print("Level of difficulty is " + descriptions[i])
break

How to get the code to make the user pick between different ranges of random numbers in python? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I want to make a code that generates random numbers from 3 different choices that the user can pick from but the code just doesn't recognize the variable I put to represent those choices. Here is what I've got so far:
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num == random.randint(1, 10)
elif choose == 2:
num == random.randint(10, 50)
elif choose == 3:
num == random.randint(1, 100)
else:
print("Invalid input")
print(num)
Can someone help?
There is some confusion. == is for comparison like is a equal to b? and = is for assignment. So, just replace to this num = random.randint(10, 50).
Also, you should consider moving print(num) inside the if...elif...else statements as it can raise an error when invalid input is provided.
import random
choose = int(input("1, 2 or 3? "))
if choose == 1:
num = random.randint(1, 10)
print("Your Number: "num)
elif choose == 2:
num = random.randint(10, 50)
print("Your Number: "num)
elif choose == 3:
num =random.randint(1, 100)
print("Your Number: "num)
else:
print("Invalid input")

Number guessing Guessing Game [closed]

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
I made a simple number guessing Game and it works perfectly fine, but I want to add something that says "The Number you have entered is too high/ low", because when I type in 100 as my upper bound it is much too difficult to guess the number.
import random
while True:
flag = True
while flag:
num = input('Enter an upper bound: ')
if num.isdigit():
print("Let's Play!")
num = int(num)
flag = False
else:
print('Invalid input! Try again!')
secret = random.randint(1,num)
guess = None
count = 1
while guess != secret:
guess = input('Please enter a number between 1 and ' + str(num) + ": " )
if guess.isdigit():
guess = int(guess)
if guess == secret:
print('Right! You have won!')
else:
print('Try again!')
count += 1
print('You needed', count, 'guess(es) ')
Alright, I'm not gonna solve it for you, but I'll give you a hint. This seems like a homework problem, so it would be unethical of me to provide you with a solution.
else:
print('Try again!')
count += 1
Look at this else statement here. What is the purpose of this else statement? To tell the user they got the guess wrong.
Think about how you can put an if/else condition inside this else condition, to tell the user if their input is too high, or too low.

Can you define a variable inside an if statement? [closed]

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 am trying to create a variable inside an if statement but it won't allow it. Is there any other way to do what I'm trying?
print ('Type numbers 1 or 2')
c = int(input())
if c == 1:
answer = (90) #so the variable "answer" will be set to '90' if the user types "1"
if c == 2:
answer = (50)
print (answer)
The code will run in the expected way if either 1 or 2 are entered as input. But if user inputs another number, you will get an Exception:
NameError: name 'answer' is not defined
To avoid this, you could declare the variable answer before the if statements:
answer = 0 # or a reasonable number
if c == 1:
answer = 90
# ...
Note: The () in answer = (90) are not necessary since it's a single number.
You need to allow the possibility that your input() might be out of your preconceived options:
c = int(input("Type no. 1 or 2: "))
if c==1:
answer=90
print(answer)
elif c==2:
answer =50
print(answer)
else:
print("you input neither 1 nor 2")
Here's a more compact solution using assert:
c = int(input("Type no. 1 or 2: "))
assert c in [1,2], "You input neither 1 nor 2."
answer = 90 if c == 1 else 50
print(answer)

Categories

Resources