Im fairly new to coding, and Im practicing along the Automate the boring stuff with python book when I came across the collatz sequence problem.
As you guys can see from my code, in the 1st time, I typed "puppy" and it said "NameError" and ended the run altogether without returning to the input phase. In the 2nd time, I typed "89", and then "puppy", and it printed out the valueerror text along with the result from last time.
How can I fix this for both the times? Thank you guys.
Basically, the value of 268 came from the last calculation and wasn't overwritten due to the exception caught with "puppy". To fix that, simply use the continue keyword to loop back to the "try" condition:
except ValueError :
print('You must enter an integer')
continue
Related
This is my code in python3:
import heapq
myQueue = []
n = raw_input()
try:
num = int(n)
if num<=100000 :
arr = input().split()
for i in range(num):
heapq.heappush(myQueue, arr[i])
print(myQueue[0])
except (NameError, ValueError):
print("Wrong Input, N should be under 100000")
except IndexError:
print("Inputs is less than actually required")
except EOFError:
print ("Error: EOF or empty input!")
I am trying to implement priority queue.
But I am facing this EOF Error while solving this problem on GUVI.
Output:
Error: EOF or empty input!
The tried to catch the error using except EOFError, but that will just make my program run but does not solve input problem right.
I even tried to run this piece of code on Sublime text editor as well as Vs code,
where it runs just fine, Correct Output.
I don't understand, is there a problem in my code or That Online Platform.
I even tried to search the answer on their Q&A platform of GUVI, I found similar question but no one has answered it.
And this is not just for this piece of Code but I found the same error for many before.
Could ANYONE help me, Please!
Thanking you in advance.. :)
EOF error occurs if the input is not provided.
In case of most of the online compilers you need to provide the inputs before running the code.
With that said, while your are trying to access the input via raw_input() there will be no input supplied resulting in the above error.
To avoid this, error supply inputs before running your code like below
Also, I can notice that you are using raw_input() and input(). Please note that raw_input() can be used if you are using Python 2 and input() if you are using python 3 correspondingly.
This piece of code is supposed to find account balance after withdraw from bank(fee=0.5).
wd,ac=raw_input().split(" ")
wd=int(wd)
ac=float(ac)
if(ac<wd):
print(ac)
elif(wd%5==0):
if(ac>wd+0.50):
print(ac-wd-0.50)
else:
print(ac)
else:
print(ac)
I got a Runtime NZEC Error while submitting on codechef. I am newbie and had searched for resolve and had used split(" ") in place of int(input()), which I had tried previously, but the problem still occurs.
Geeksforgeeks says:
"In python, generally multiple inputs are separated by commas and we read them using input() or int(input()), but most of the online coding platforms while testing gives input separated by space and in those cases int(input()) is not able to read the input properly and shows error like NZEC"
Given that I've tried to account for that... What is wrong with my code?
It looks like an error with your raw_input statement. Remember that, in python 3, raw_input doesn't exist any more. If you changed it from raw_input to input, then the code works just fine.
I'm new to programming and don't know what this means. Please tell me what to do to my code for my assessment. It doesn't highlight the problem when I run it and I don't know what to do. Please help me.
To be honest, reading a Python tutorial would probably do you better. In any case, however...
Python is an indentation-based language. This means anytime you enter a new "block" (code that belongs to a statement), you need to indent your code by one level. Here are two examples, one incorrect and one correct:
if 5 < 10:
print "5 is less than 10! Wow! Thanks, math!"
print "I'm so glad Python told me."
This is a syntax error, since the print statement belongs to the if statement, and is therefore a new block. It should be indented, but in this case it wasn't, so it's an error.
if 5 < 10:
print "5 is less than 10! Wow! Thanks, math!"
print "I'm so glad Python told me."
print "This is printed in any case, since it doesn't belong to the above block."
Here is the fixed verion. Notice the four spaces in the beginning of the second line? That's called "indentation". Any subsequent lines indented to the same level will be part of the block. Generally, you press TAB to indent in your text editor. The last line, however, is not indented and will therefore run regardless of whether the if statement evaluates to True or not.
I am a total newbie in programming so I was hoping anyone could help me. I am trying to write program in python that, given an integer n, returns me the corresponding term in the sylvester sequence. My code is the following:
x= input("Enter the dimension: ")
def sylvester_term(n):
""" Returns the maximum number of we will consider in a wps of dimension n
>>> sylvester_term(2)
7
>>> sylvester_term(3)
43
"""
if n == 0:
return 2
return sylvester_term(n-1)*(sylvester_term(n-1)-1)+1
Now, my questions are the following, when trying to run this in GitBash, I am asked to input the n but then the answer is not showing up, do you know what I could do to receive the answer back? I plan to continue the code a bit more, for calculating some other data I need, however, I am not sure if it is possible for me to, after coding a certain piece, to test the code and if so, how could I do it?
You will need to add:
print(sylvester_term((int(x)))
to the end of your program to print the answer.
You will need to cast to int because the Python Input() function stores a string in the variable. So if you input 5 it will return "5"
This does not handle exceptions, e.g if the user inputs a letter, so you should put it in a try and except statement.
Here's an example of how I'd handle it. You can use sys.argv to get the arguments passed via the command line. The first argument is always the path to the python interpreter, so you're interested in the second argument, you can get it like so:
sys.argv[1]
Once that is done, you can simply invoke your function like so
print(sylvester_term(int(sys.argv[1]))
Hello I am a fairly novice programmer. And I have a simple code
name=raw_input("Hello I am Bob. What is your name?")
print("It is very nice to meet you,", name)
response=raw_input("what do you want to do today?")
if response == "price match":
However on the fourth line I get SyntaxError: unexpected EOF while parsing error and I did look into it and I found that using the raw_input for inputted strings is much better than using the input function.I don't know why the error keeps popping up. Could I get some help and perhaps some suggestions as to how I can improve the code.
You have to do something in the if statement. For example, print a price:
if response == "price match":
print "Yes, we can do that for you".
But, you can't just leave a block (the stuff that is indented after a :) empty, or Python will give you an error.
In rare cases (and not in your case here), you may want to do absolutely nothing, but still have a block (e.g. if required to by an except:). In that case, you still have to put something in the block, but you can use the word pass which does nothing. (This is almost never used in an if block).
I'm no Python expert.. But it looks like your problem is here:
response=raw_input("what do you want to do today?") if response == "price match":
You are defining an if statement, but if this value is true, how do you handle it?
I'm guessing you would need something like:
if response == "price match":
print('Match')
else:
print('Did not match')
Hope this helps a little
If you need a placeholder for just having valid syntax before putting in the body of your if/else/functions/etc, use pass.
http://docs.python.org/release/2.5.2/ref/pass.html
As for your code, a valid if statement must always have a body. So put some code there or use pass.