User inputs reserved keyword in Python. There happens an error - python

I am working on a simple program (just for a joke). A program wants that the user input yes or no (it can be in different languages). But when he enters a reserved word (i. e. keyword) there happens an error because this keyword does some bugs in the code.
My truncated code (maybe it seems unclear because it's truncated):
x = input('Enter yes or no (you can do this in different languages...) ')
x = x.lower()
answersYes = ['yes','si','oui','ioe','inde','tak','ja','da']
answersNo = ['no','ayi','che','leai','nie','ne','nein']
if ' ' in x:
print('Input just one word!')
else:
if x in answersYes:
print('You enteres YES!')
elif x in answersNo:
print('You enteres NO!')
else:
print('Sorry, but this isn\'t YES nor NO!')
I have done some googling around, but there was no luck yet.
Thank you a lot for any answer!
P.S.
Just one little note:
When I have run the upper script in Python in basic Python IDLE, there wasn't any error, but when I have run this in Spyder, there displayed this message (when I typed 'yes in no' ("in" is a reserved word)):
File "<ipython-input-49-d1e48c3ddecb>", line 1
yes in no
^
SyntaxError: invalid syntax

I don't get an error. Make sure your spyder is python3-configured or try running with python3 file.py
You can also try replacing input() with raw_input()

Related

Detecting the input of a carriage return in the console

A requirement for a school assignment I have asks that when a carriage return (\r) is entered as an input, it is used to halt the program: "if only a carriage-return is entered, halt the program". I've tried several things, but am stumped as to the solution.
Below are the options I've checked, all of which fail the running tests.
while input != '':
while input != '\r':
while input != '\n':
Thanks!
input is a function. You have to write input().
However, I assume you are actually trying to read information from the user, and exit if they entered nothing. Your statement while input() != '': won't do that, because if they enter something useful, it will be thrown away. You haven't stored it.
You PROBABLY want something like:
while 1:
line = input()
if not line:
break
# Now use the contents of line.

Python3 NZEC Error

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.

Python Blackjack Run module IDLE error?

I try to run the following module written for a blackjack 21 game in Python but get an error saying:
Unindent does not match any outer indentation level.
Questions
Please help resolve the error.
Also is there a stable option to run modules and get them checked with a python checker which I know there is but an easy to use one without adding code in python , just running it to check where the code got broken?
example
if the screenshot is unclear:
koloda = [6,7,8,9,10,2,3,4,11] * 4
import random
random.shuffle(koloda)
print(' Lets play blackjack, shall we? ')
count = 0
while True:
choice = input(' Will you pick another card? y/n\n ')
if choice == ' y ':
current = koloda.pop()
print(' You got a higher card %d ' %current)
count += current
if count > 21:
print(' Sorry, you lost ')
break
elif count == 21:
print(' Congrats, you got 21! ')
break
else:
print(' You have %d points. ' %count)
elif choice -- 'n':
print(' You have %d points and you ended this game. ' %count)
break
print('See you again!')
Thanks in advance
Your indentations are inconsistent. In python, you can choose how you indent blocks (e.g. tabs, 4 spaces, 3 spaces...), but you should always stick to your initial choice. Otherwise you'll get indentation errors.
Regarding your other question, there are many IDEs that will report syntax errors (such as the ones in your code) while writing. I like Pycharm, but you can choose anything you like. PyCharm also has an option to convert indentation between spaces and tabs, which can help you fix your code.

SyntaxError: unexpected EOF while parsing in python

I am getting the above error in python.
w=[]
h=[]
l=int(input())
t=int(input())
for i in range(t):
n=raw_input()
w.append(n)
n=int(input())
h.append(n)
for i in range(t):
if w<l and h<l:
print ("UPLOAD ANOTHER")
elif w>l and h>l and w!=h:
print ("CROP IT")
elif w>=l and h>=l and w==h:
print("ACCEPTED")
You probably given a empty input or '\n' for the n=int(input()) part. Try to give the number as input.
put this line in your terminal
x = int(input()). and give empty input, (just tap enter). vola..
SyntaxError: unexpected EOF while parsing
You might be running the script on some online IDE. This also appeared to me while I was submitting my solution. The best way to deal with this is don't run your script on online ide rather directly submit it.
If the answer is correct it would get accepted else show the wrong answer.

How to verify numbers in input with python?

I'm trying to learn how to program and I'm running into a problem....
I'm trying to figure out how to make sure someone inputs a number instead of a string. Some related answers I found were confusing and some of the code didn't work for me. I think someone posted the try: function, but it didn't work, so maybe I need to import a library?
Here's what I'm trying right now:
Code:
print "Hi there! Please enter a number :)"
numb = raw_input("> ")
if numb != str()
not_a_string = int(next)
else:
print "i said a number, not a string!!!"
if not_a_string > 1000:
print "You typed in a large number!"
else:
print "You typed in a smaller number!"
Also I have another question while I'm asking. How can I make it so it will accept both uppercase and lower case spellings? In my code below, if I were to type in "Go to the mall" but with a lowercase G it would not run the if statement because it only accepts the capital G.
print "What would you like to do: \n Go to the mall \n Get lunch \n Go to sleep"
answer = raw_input("> ")
if answer == "Go to the mall":
print "Awesome! Let's go!"
elif answer == "Get lunch":
print "Great, let's eat!"
elif answer == "Go to sleep":
print "Time to nap!"
else:
print "Not what I had in mind...."
Thanks. ^^
Edit: I'm also using python 2.7 not 3.0
You can do something like this:
while True: #infinite loop
ipt = raw_input(' Enter a number: ')
try:
ipt = int(ipt)
break #got an integer -- break from this infinite loop.
except ValueError: #uh-oh, didn't get an integer, better try again.
print ("integers are numbers ... didn't you know? Try again ...")
To answer your second question, use the .lower() string method:
if answer.lower() == "this is a lower case string":
#do something
You can make your string comparisons really robust if you want to:
if answer.lower().split() == "this is a lower case string".split():
In this case, you'll even match strings like "ThIs IS A lower Case\tString". To get even more liberal in what you accept, you'd need to use a regular expression.
(and all this code will work just fine on python2.x or 3.x -- I usually enclose my print statements in parenthesis to make it work for either version).
EDIT
This code won't quite work on python3.x -- in python3, you need to change raw_input into input to make it work. (Sorry, forgot about that one).
First,you should ask only one question per post.
Q1: use built-in .isdigit()
if(numb.isdigit()):
#do the digit staff
Q2:you can use string.lower(s) to solve the capital issue.
you may try
numb = numb.strip()
if numb.isdigit() or (numb[0] in ('+', '-') and numb[1:].isdigit():
# process numb

Categories