How to enter a input without pressing enter [duplicate] - python

This question already has answers here:
raw_input without pressing enter
(11 answers)
Closed 9 years ago.
For instance I would like it so,
x = int(input("Please enter your guess :")
If x = 1:
Print ("correct")
Although each time i need to press enter, how could I make it so it would read on key press.
I did find getch() although i could not get it to work.

In Windows you can use msvcrt. On other systems its a bit more difficult, take a look at this recipe: http://code.activestate.com/recipes/134892/
You can use it that way:
getch = _Getch()
print 'Please enter your guess: '
x = getch()
if (int(x) == 1):
print 'correct'
else:
print 'wrong'

Related

Create a conversation that has a Y/N answer [duplicate]

This question already has answers here:
How do I do a case-insensitive string comparison?
(15 answers)
Closed 6 days ago.
I am trying to create a small bit of code in Python that runs a conversation, then asks a Y/N question i.e "Do you believe the sky is blue?"
When I run the code, it works well until I reach the question. It ignores my parameters for Y/N answers to print specific responses. It gives me the print response attached to my "else" statement.
I am confused if I am writing my If/elif/else statements wrong?
My code is written as follows:
x = input('do you believe the sky is blue')
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.)
You use .lower() to obtain a lower case version of a string, and you are missing a ' in the last print():
x = input('do you believe the sky is blue ').lower()
if x == "yes":
print("I believe you are correct")
elif x == "no":
print('I think you have a unique perspective.')
else:
print('Please answer Yes or No.')

why is the while loop not breaking? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 months ago.
chosenNumber = input ("Choose A Number: ")
if chosenNumber.isdigit():
chosenNumber = int(chosenNumber)
else:
while chosenNumber.isalpha():
if chosenNumber == chosenNumber.isdigit():
chosenNumber = int(chosenNumber)
break
input ("Please Choose A Number: ")
I tried to make it so if the input is not a number it will keep asking him to choose a number but the while loop just keeps going is there a fix for that?
Consider rewriting whole part like this (without if and double input):
chosen_number = ''
while not chosen_number.isdigit():
chosen_number = input("Please Choose A Number: ")
chosen_number = int(chosen_number)
you don't change this variable in your code, so it always contains first input

Problem with repeating program in python3 [duplicate]

This question already has answers here:
How to test multiple variables for equality against a single value?
(31 answers)
Closed 2 years ago.
How do i get this program repeat when 'again' gets 'Y' or 'y'? Yesterday the same code worked smh, but today it closes the program whatever i write in there) And yeah...tabulation is wrong but it's because stackoverflow copied it in some weird way :))
while True:
start = input("What do you want to do? + - * / ")
if start == '+':
x = float(input("digit 1 "))
y = float(input("digit 2 "))
res = x + y
print('The result is ' + str(res))
again = input('Do u want to try again? Y/N ')
if again == 'N' or 'n':
break
Look, you are using the wrong syntax for checking the condition. Use this syntax :
if again=='N' or again=='n':
break

The if statement goes automaticly to else statement [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 3 years ago.
When I run the code and answer with 3, the console shows The answer is 3. This code is just an example, I worked on a code with random number.
I gave the input in the if statement, in a variable and i deleted else statement
answer = input("Answer of 1 + 2 = ")
if answer == 3:
print("You're right!")
else:
print("The answer was 3")
The right output would be You're right!
Typecasting to the rescue!
answer = input("Answer of 1 + 2 = ")
if int(answer) == 3:
# ^^^
print("You're right!")
else:
print("The answer was 3")
By default the value you get from input() is of type string. If you write 3 in your console, you get
answer = "3"
and
"3" != 3
You need to cast the input to int. Add this line before the if statement:
answer = int(answer)
Be careful to check that the typed value is actually an int (you can do that by using a try catch statement, or better with a while)

If we don't know the number of entries then how to stop a input() loop of while or for [duplicate]

This question already has answers here:
how to stop a for loop
(9 answers)
Closed 4 years ago.
I tried to get input by bigger number entries but it says "index out of range"
if we don't know the number of entries then how to stop a input() loop of while or for, in python:
while():
listMatch.append(input())
Look what if I have:
Djokovic:Murray:2-6,6-7,7-6,6-3,6-1
Murray:Djokovic:6-3,4-6,6-4,6-3
Djokovic:Murray:6-0,7-6,6-7,6-3
Murray:Djokovic:6-4,6-4
Djokovic:Murray:2-6,6-2,6-0
Murray:Djokovic:6-3,4-6,6-3,6-4
Djokovic:Murray:7-6,4-6,7-6,2-6,6-2
Murray:Djokovic:7-5,7-5
Williams:Muguruza:3-6,6-3,6-3
AND
Halep:Raonic:2-6,6-7,7-6,6-3,6-1
Kerber:Raonic:6-3,4-6,6-4,6-3
Raonic:Wawrinka:6-0,7-6,6-7,6-3
Wawrinka:Raonic:6-4,6-4
Halep:Raonic:2-6,6-2,6-0
Wawrinka:Raonic:6-3,4-6,6-3,6-4
Raonic:Wawrinka:7-6,4-6,7-6,2-6,6-2
Wawrinka:Kerber:7-5,7-5
Halep:Kerber:3-6,6-3,6-3
Halep:Wawrinka:0-6,0-6,6-0,6-0,7-5
Kerber:Wawrinka:6-3,4-6,7-6,0-6,7-5
Here I don't know how many entries I want to enter. In above examples there are 9 and 11.
This loop will stop as soon as you just press return (no string entered)
listMatch=[]
while True:
z=input()
if z:
listMatch.append(z)
else:
break
add a control loop that checks for input:
listMatch =[]
while(True):
input_value = input()
if(input_value == '' or input_value == 'quit'):
break
listMatch.append(input_value)
if no text or quit is entered, loop will break
listMatch = []
while True:
# get the user's input
stuff = input()
# if they typed "quit", stop looping
if stuff == 'quit':
break
# otherwise append their input to the list and keep on looping
listMatch.append(stuff)

Categories

Resources