This question already has answers here:
convert txt file with mixed spaces/tabs to tabs only (where possible) [closed]
(7 answers)
Closed 4 years ago.
This is a small game I made. Simple rules, the player must determine the number in my mind. I will later import random module though.
print('Hello!, What is your name?')
userName = input()
print(myName + 'Welcome to Guess it! Ralph')
print('You must guess the number that I have in my mind(from 1 to 10) within 5 chances')
myNumber = 3
print('Give your prediction')
for no_of_chances in range(5, 0, -1):
usersPrediction = input()
I get this error when I run in pycharm:
if usersPrediction < myNumber:
^
IndentationError: unexpected indent
if usersPrediction < myNumber:
print('It is lower than required,try a higher value')
elif usersPrediction > myNumber:
print('It is higher than required,try a lower value')
elif usersPrediction == myNumber:
print('yahoo!, you did it')
print('You always have a next chance, try it again')
Indent print('yahoo!, you did it')
Related
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 3 years ago.
I am making a fizzbuzz game [see code below] I wanted to add a condition to the if statement that would loop the user back to the beginning of the function if they enter anything other than a number but it did not work it gave me a error anyway is there a way I can go about doing that?
def fizzbuzz():
print(f"\nEnter a number {name}.\n")
number = int(input('> '))
if number % 3 == 0:
print("We got a FIZZ! And I ain't talking about the kid from B2k! You a bad MF!")
elif number % 5 == 0:
print("Buzz lightyear to star command. We are in the mist of a BAMF!")
elif number % 3 == 0 and number % 5 == 0:
print("FIZZBUZZ!" * 10, " FIZ [*head explodes*] [kid picks up the mic] How's that for Television")
elif number / number != 1 or number == 0:
print("Someones a little slow. Try again sweetheart.")
fizzbuzz()
else:
print("Do you kiss your mother with that none Fizzed or buzzed mouth? Shame! Sercurity!")
print("Join us next time for the show where you better bring your fizzbuzz buzz fizz!......Or else.\n\n")
Try using try and except to handle the errors.
This question already has answers here:
Python - User input data type
(3 answers)
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
I am very new to programming and Python. To get started I'm working on a little game that will ask the user for some input and then do "something" with it. My problem is I've seem to account for if the user types in an int lower or high than my parameters BUT i can't seem to find a way to re-prompt the user if they type in anything but an int.
With my limited knowledge I thought that when using an if/elif/else statement if you didn't define what the if/elif is looking for than the else statement was there for everything else that you didn't account for?
Looking for some more insight on how to master this fundamental concept
Thank you in advance!
prompt = True
while prompt == True:
user_input = input("Please give me a number that is greater than 0 but less than 10 \n >")
if user_input > 0 and user_input <= 10:
print("Good job " + str(user_input) + " is a great number")
break
elif (user_input > 10):
print("Hey dummy " + str(user_input) + " is greater than 10")
elif (user_input <= 0):
print("Hey dummy " + str(user_input) + " is less than 0")
else:
print("I have no idea what you typed, try again!")
How about something like this?
a = -1
while a < 0 or a > 10:
try:
a = int(input("Enter a number between 0 and 10: "))
except ValueError:
continue
This will only allow the user to enter an int from 0 to 10, this will also remove the need to print those messages if the number is outside of this range, if you would like to keep those messages I could make adjustments and show you how to handle that as well
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 4 years ago.
Another newbie question:
I'm trying to add a statement inside a while loop that if the person enters anything except integer it will repeat the input but I didn't figure out how to do that without ruining the program. Whenever I enter anything i get the following error: "ValueError: invalid literal for int() with base 10" What is needed to be added to my code?
Here is my code:
import random
#Playing dice game against the computer
num = int(input("Enter a number between 1 and 6 please: "))
while not int(num) in range(1, 7):
num = int(input("Please choose a number between 1 and 6: "))
def roll_dice(num):
computer_dice = random.randint(1, 6)
if num > computer_dice:
print("Congratulations you win! Your opponent's dice is:", computer_dice)
elif num < computer_dice:
print("Sorry but you lose! Your opponent's dice is:", computer_dice)
else:
print("Draw. Your opponent's dice is:", computer_dice)
roll_dice(num)
Thank you in advance!
I think the problem is that you are trying an empty string to an integer. Same problem when you type in alphabetical characters.
You can use try and except to try the conversion of the input to an integer and then when it failed you run the loop again and when the conversion was successfully you have your number.
This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 4 years ago.
Why if i use OR at !!! section, does it break even if i type a number 1 - 9 in the guess input. And why do i not need to type BOTH 'q' and 'quit', because thats what i assume AND means... 'q' AND 'quit'...
import random
while True:
print("\nGuess a number between 1 and 9.")
guess = input("I guess: ")
if guess == 'q' !!!or!!! 'quit':
break
number = random.randrange(1, 10)
try:
if int(guess) < number:
print(f"You guess was too low. The number was {number}")
elif int(guess) > number:
print(f"Your guess was too high. The number was {number}")
elif int(guess) == number:
print(f"Your guess was exactly right! The number was {number}")
except ValueError:
print("Please only guess numbers!")
So with OR it doesn't work and with AND it does. This makes no sense to me. Why is this?
if guess == 'q' or 'quit':
This statement will not work because you are trying to use a string as a boolean. The OR doesn't assume you want the exact same thing to happen like before it, you have to fill the condition again.
if guess == 'q' or guess == 'quit':
This will work because you are now getting a boolean out of the right side instead of just trying to use a string.
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I'm new here, and I'm also new in coding.
I'm actually learning Python, and I have a question, because I already tried everything, but I was unable to resolve it.
I have this code from a little game I saw in a tutorial. The objective is to the user guess the number. What I was trying to do is to handle the exception if the user enters a letter, then show an error message and go back to the loop. If someone helps me, I will be grateful.
import random
highest = 200
answer = random.randrange(highest)
guess = raw_input("Guess a number from 0 to %d:" %highest)
while(int(guess)!=answer):
if (int(guess) < answer):
print "Answer if higher"
else:
print "Answer is lower"
guess=raw_input("Guess a number from 0 to %d: " %highest)
raw_input ("You're a winner Face!!!")
This is how i would do it:
import random
highest = 200
answer = random.randrange(highest)
while True:
try:
guess = int(input("Guess a number from 0 to %d: " %highest))
if guess < answer:
print("Answer if higher")
elif guess > answer:
print("Answer is lower")
else:
print("You're a winner Face!!!")
break
except:
print('Input not valid!')
continue
I have a dummy condition on the while and i am directing the flow from inside the loop using continue and break. I wrapped the whole guess checking procedure in a try-except block but the only thing that is really tried is the conversion of the input to integer. Everything else could also be moved after the except bit.