python-2.x

Python how to only accept numbers as a input

守給你的承諾、 提交于 2019-12-02 08:24:50
mark= eval(raw_input("What is your mark?")) try: int(mark) except ValueError: try: float(mark) except ValueError: print "This is not a number" So I need to make a python program that looks at your mark and gives you varying responses depending on what it is. However I also need to add a way to stop random text which isn't numbers from being entered into the program. I thought I had found a solution to this but it won't make it it past the first statement to the failsafe code that is meant to catch it if it was anything but numbers. So pretty much what happens is if I enter hello instead of a

passing string to file open function in python

∥☆過路亽.° 提交于 2019-12-02 07:51:01
问题 I have a user input and I want to pass it as the file name parameter of the open function. This is what I have tried: filename = input("Enter the name of the file of grades: ") file = open(filename, "r") When the user input is openMe.py an error arises, NameError: name 'openMe' is not defined but when the user inputs "openMe.py " it works fine. I am confused as to why this is the case because I thought the filename variable is a string. Any help would be appreciated, thanks. 回答1: Use raw

passing string to file open function in python

若如初见. 提交于 2019-12-02 04:24:26
I have a user input and I want to pass it as the file name parameter of the open function. This is what I have tried: filename = input("Enter the name of the file of grades: ") file = open(filename, "r") When the user input is openMe.py an error arises, NameError: name 'openMe' is not defined but when the user inputs "openMe.py " it works fine. I am confused as to why this is the case because I thought the filename variable is a string. Any help would be appreciated, thanks. Use raw_input in Python 2: filename = raw_input("Enter the name of the file of grades: ") raw_input returns a string

How do I get user input from the keyboard in Python 2?

北战南征 提交于 2019-12-02 02:30:37
问题 I wrote a function in Python which prompts the user to give two numbers and adds them. It also prompts the user to enter a city and prints it. For some reason, when I run it in a shell, I get "name is not defined" after I enter the city. def func_add(num1, num2): a = input("your city") print a return num1 + num2 回答1: If you're on Python 2, you need to use raw_input : def func_add(num1, num2): a = raw_input("your city") print a return num1 + num2 input causes whatever you type to be evaluated

How do I get user input from the keyboard in Python 2?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 02:13:43
I wrote a function in Python which prompts the user to give two numbers and adds them. It also prompts the user to enter a city and prints it. For some reason, when I run it in a shell, I get "name is not defined" after I enter the city. def func_add(num1, num2): a = input("your city") print a return num1 + num2 If you're on Python 2, you need to use raw_input : def func_add(num1, num2): a = raw_input("your city") print a return num1 + num2 input causes whatever you type to be evaluated as a Python expression, so you end up with a = whatever_you_typed So if there isn't a variable named

Subpackages and relative imports in PyCharm

。_饼干妹妹 提交于 2019-12-01 22:19:43
I am using python 2: python --version Python 2.7.13 :: Continuum Analytics, Inc. I have the following project structure: . └── foo ├── bar1 │ ├── __init__.py │ └── mod1.py ├── bar2 │ ├── __init__.py │ └── mod2.py ├── __init__.py └── start.py start.py from foo.bar2.mod2 import mod2_f mod2_f() mod1.py def mod1_f(): print "mod1_f" mod2.py from foo.bar1.mod1 import mod1_f def mod2_f(): mod1_f() print "mod2_f" If I run start.py from an IDE things work ok. However using something like this: python ./foo/start.py results in Traceback (most recent call last): File "./foo/start.py", line 1, in <module>

In Python 2, can I pass a list to the percent-format operator?

对着背影说爱祢 提交于 2019-12-01 21:44:01
I have a list of things I'd like to print using a format string. In Python 3-style, using "a string".format(arg,arg,arg) , this is easy. I can just replace with arguments with *mylist , like mylist = [1,2,3] "{} {} {}".format(*mylist) I can't seem to make this work with the older percent-formatting. I've tried stuff like "%i %i %i" % mylist , %i %i %i" % (mylist) , and %i %i %i" % (*mylist) but I just keep getting syntax errors or "not enough arguments". Is this impossible in 2.x style? ETA: I am changing the example above, I actually did mean list , not tuple . I'm still learning and I'm not

What is the Python <> operator

柔情痞子 提交于 2019-12-01 21:32:11
What exactly is the <> operator in Python, and why is it undocumented (as far as I can tell)? Is it the same as != or is not ? In Python 2.x , <> is the same as != (i.e. "not equal to" , rather than is not which is "not identical to" ), but the latter is preferred: The comparison operators <> and != are alternate spellings of the same operator. != is the preferred spelling; <> is obsolescent. In 3.x, <> has been removed and only != exists. It is documented, but you're not supposed to use it. Your guess about it being equivalent to != is correct. Quoting the Python 2 documentation : != can also

How do I prevent my Python application from automatically closing once reaching the end of code? [duplicate]

旧时模样 提交于 2019-12-01 21:16:05
This question already has an answer here: How to keep a Python script output window open? 19 answers I'm new to programming, especially Python. I'm trying to make an application that converts Fahrenheit to Celsius, but I don't know how to make the program stay open. Whenever it reaches the end of the code, it automatically closes before the user can see his or her results. I'm using Python 2.6. Well, I guess you mean the terminal that Windows opens for you when you run a python file is closed too fast. You can add raw_input('Press Enter to exit') right before your program would exit. It tells

How to convert hexadecimal string to character with that code point?

孤街醉人 提交于 2019-12-01 19:32:29
I have the string x = '0x32' and would like to turn it into y = '\x32' . Note that len(x) == 4 and len(y) == 1 . I've tried to use z = x.replace("0", "\\") , but that causes z = '\\x32' and len(z) == 4 . How can I achieve this? You do not have to make it that hard: you can use int(..,16) to parse a hex string of the form 0x... . Next you simply use chr(..) to convert that number into a character with that Unicode (and in case the code is less than 128 ASCII) code: y = chr(int(x,16)) This results in: >>> chr(int(x,16)) '2' But \x32 is equal to '2' (you can look it up in the ASCII table): >>>