This question already has answers here:
How can I read inputs as numbers?
(10 answers)
What's the canonical way to check for type in Python?
(15 answers)
Closed 2 years ago.
I'm working on a script to take user input and write it into an Excel file
currently the script works but when trying to add in logic to reject non-integer answers, one of two things happen:
it gets stuck in an infinite loop after properly rejecting the first answer where it keeps asking for a new answer and never accepts it
it properly rejects the first answers, but then accepts the second no matter what it is
this is the code being used where it gets stuck in an infinite loop (I have tried a few other variations that do the same thing but this is the most recent)
true = True
x = input('What is the first marked distance?')
while true == True:
if x == int:
cl.value = x
break
else:
x = input('Please enter a whole number')
any advice on why this is looping and not working would be greatly appreciated
Use float(x)%1 == 0 instead of x == int
Related
This question already has answers here:
It is more efficient to use if-return-return or if-else-return?
(9 answers)
Closed 6 months ago.
I've always had the habit to put else statements but I'm wondering if there's any reason not to, if it works.
Here's an example:
def multiply_by_two(number, display_original_number=False):
result = number*2
if display_original_number is True:
return f'Multiplying {number}: {result}'
return f'{result}'
print(multiply_by_two(5, True))
print(multiply_by_two(5))
Output:
Multiplying 5: 10
10
If I put else: return f'{result}', it's bascially the same thing, right? Is there any reason I would want to put an else statement in such cases?
Within this use case there is no difference functionally. Personal preference would dictate this decision if you find it easier to read back your code using else: .
This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Comparing a string to multiple items in Python [duplicate]
(3 answers)
Closed 2 years ago.
Note: I'm not a fluent english speaker, so you may see some grammar mistakes here. I'm kinda new to programming and i know i can fix it by putting another else/elif statement, i just trying to get the short possible lines to avoid spamming elif like yandev.
so the thing is, at the #functions i already put if below the gae variable's input, which says that if the input = y or Y then it runs the line below it, which is print("good. go to www.minecraft.net"). Now my problem is, when i try to input other letter than Y, it still runs the same line whilst there's already an else line underneath that i think should exclude other words than Y or y. For example, i already input the word "n" at the output console(?) but the output says "good. go to www.minecraft.net" instead the lines at the else statement. is there any possible solution without adding much more elif/else statement?
Click this link for the image to understand what am i talking about
The line if gae == "Y" or "y" should be if gae == "Y" or gae == "y" otherwise it will always be true and the code will always print "good. go to www.minecraft.net".
This question already has answers here:
Why can't "i" be manipulated inside for-loop [duplicate]
(3 answers)
Closed 3 years ago.
I just have a basic question about a for loop in Python 3. It's a stupid, simple question, but I figure everyone else here is far more knowledgeable to me, and most answers are pretty bright! I do not profess to be intelligent, so I'd like some clarification. Running this loop seems to add a value of only one. I have defined x to be 1, so this makes sense, but I didn't give i a true value and it seems to just naturally be 1.
for i in range(0, 50):
x = 1
i = x + i
print(i)
I'm not getting an error, I'm just curious as to what's going on behind the scenes. Would love some explanation from someone who understands this more than I do!
You do not need the x=i and i=x+i inside your loop.
for i in range(0, 50):
print(i)
would work just fine and would output 0,1,2,3...47,48,49. i is being set according to the range(0,50). If you want i to start at 1 simply change the range to range(1,50). Your code also added 1 to each i value so in order to account for this in the range you would need to do range(1,51) which would print 1,2,3...48,49,50.
Further Reading
I would recommend reading these to better understand how for loops work and also more info about ranges:
Range()
For Loops
This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
Basically, I have a "raw_input" then an "if" statement and then many "elif" statements after it. If the user inputs invalid data I want the "else" to print a string, then restart the program so the user can try again. How can I do this?
Put your code in a loop:
while True:
# Code here
To exit the loop, use break, which will also quit the program.
while True:
# Code here
if running == False: # The condition for breaking is up to you, if you're using one
break
This question already has answers here:
How to read a single character from the user?
(26 answers)
Python sys.stdin.read(1) in a while(True) loop consistently executes 1 time getting input and multiple times not getting input
(2 answers)
Closed 6 years ago.
I'm writing a Python program (on OS X/Linux) in which a user has to type some words in a while loop. This is the relevant part of it:
i = 0
array = []
while (i < 10):
array.append(raw_input("Prompt: "))
i = i + 1
However I'd like to stop the input as soon as the user types a SPACE, without the need to press ENTER.
I know I can't do this using raw_input, however I did not find any way to achieve what I'm trying to do. Basically I would like the user to be able to type one word after the other, using the SPACE button instead of ENTER to input the next word.
Do you have any idea? Thanks in advance.
It's not possible with raw_input, input or sys.stdin.read(1) either. There is no generic solution which works on every operating system.
Here is how to read a single character from the user in Python, without pressing Enter: Python read a single character from the user
Then you need to process each character in a for loop.