Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
My python code wont work for some reason. It says the error is coming from the syntax of the function but im not sure why its doing that
one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"
def convert()
number = raw_input('Enter the number you need converted to binary')
enterYourCommand = raw_input("Enter your command")
if enterYourCommand is "convert"
convert()
elif enterYourCommand is "tonumber"
tonumber()
You don't have : after function definition and if's:
one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
ten=10
print "test"
def convert():
number = raw_input('Enter the number you need converted to binary')
enterYourCommand = raw_input("Enter your command")
if enterYourCommand is "convert":
convert()
elif enterYourCommand is "tonumber":
tonumber()
All python functions should have a colon : at the end of their declaration line.
For example:
def convert():
number = raw_input('Enter the number you need converted to binary')
Also, the same is with your if and elif declarations:
if enterYourCommand is "convert":
convert()
elif enterYourCommand is "tonumber":
tonumber()
So, just add : at the end of each declaration and you should be good to go.
You missed the colons after if, elif and def. You have to indent with four spaces. See this link for examples.
one = 1
two = 2
three = 3
four = 4
five = 5
six = 6
seven = 7
eight = 8
nine = 9
ten = 10
enterYourCommand = raw_input("Enter your command")
def convert():
number = raw_input('Enter the number you need converted to binary')
if enterYourCommand == "convert":
convert()
elif enterYourCommand == "tonumber":
tonumber()
Hope it helps.
EDIT
Replace is with ==.
is will return True if two variables point to the same object
== will return True if the objects referred to by the variables are equal.
Sources : is-there-a-difference-between-and-is-in-python
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
library = ['Harry Potter','Lord of the rings','Lupin']
user_input=input('Choose option: ')
# When adding a book , it is not saved in library, it only prints out what was by default.
How to save books in Library?
if user_input == 'add_book':
x = input('add_book: ')
library.append(x)
elif user_input =='show_books':
for e in library:
print((e),end=' ')
# How to specify which book to remove?
elif user_input =='remove':
library.pop()
Where to place details for each book? Details should appear when book is selected.
This code resolves the problems you stated in your comments, the following points explain the changes.
library = ['Harry Potter','Lord of the rings','Lupin']
while True:
user_input = input('\n Choose option: ')
if user_input == 'add_book':
to_add = input('add_book: ')
library.append(to_add)
elif user_input =='show_books':
for e in library:
print((e),end=' ')
elif user_input =='remove_book':
to_remove = input("remove_book: ")
library.remove(to_remove)
elif user_input == 'quit':
break
else:
print("Available options: add_book, show_books, remove_book, quit")
While True lets your entire algorithm start again until user_input == 'quit', when the break command tells the program to exit the While True loop, ending the program. This was necessary to solve the problem you described of the program not saving changes: the reason was your program ended after your first input, and when you start the program again, it only runs its source code, so it couldn't show any "changes" to variables you made using it previously. Now until you type quit it "saves" every change you make.
I used library.remove() because it accepts the library key value as an argument. library.pop() required the index instead. (Now you just need to enter the item (book) name.
( I couldn't implement the last function you described)
Where to place details for each book? Details should appear when book is selected.
You didn't describe the concept of "selecting" a book and what are the "details" of one.
Hope I've been of some help.
I've figured out how to implement logic for displaying details about selected book.
elif user_input == 'book_details':
which_book = input('Which book: ')
if which_book == 'Harry Potter':
print('\n''Author: J. K. Rowling')
print('ISBN: 9780747532743')
print('Year of release: 1997')
if which_book == 'Lupin':
print('\n''Author: Maurice Leblanc')
print('ISBN: 9780141929828')
print('Year of release: 1907')
if which_book == 'Lord of the rings':
print('\n''Author: J. R. R. Tolkien')
print('ISBN: 9788845292613')
print('Year of release: 1954')
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have a simple code to get user email information, I got a problem about something :
import sys
try:
argx = sys.argv(1)
except:
print("Please Input an email.")
Example For User argument :
py main.py example#example.com
I want to take arg (input) after the # char and the . char.
I need this to check the email provider, domain name and other stuff about the email.
Example of the thing i want :
import sys
try:
argx = sys.argv(1)
x = "The argument after the char #, and the ."
except:
print("Please Input an email.")
if(x.lower() == "gmail") :
gmail()
elif(x.lower == "protonmail") :
protonmail()
You can use split function of a string in order to split a string
like
x = 'name#example.com' #Your input
email = x.split('#') # will give ['name','example.com']
provider = email[1].split('.')[0] # will give 'example'
Asuming you have the following email example#example.com. In python you can split a string with the split function. The function itself needs a delimeter. In your case the delimeter would be #. The return value of the split function is an array.
parts_of_mail = email.split("#")
>>> [example, example.com]
You now have the array parts_of_mail, which stores the left and right part of the email. You can now split the string again, like above:
provider_info = parts_of_mail[1].split(".")
>>> [example, com]
Finally you can check the provider information:
if provider_info[0].lower() == "example":
# do stuff here
elif provider_info[0].lower() == "gmail":
# do stuff here
Note: provider_info[0] stores the provider and provider_info[1] stores the domain.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm getting a vague syntax error with the print int(rollval) on line 15.
from random import randint
roll == 0
def diceroll(roll):
def dicenum(userdicenum):
userdicenum = int(raw_input("How many dice would you like to roll?"))
return userdicenum
def dicesides(userdiceside):
userdiceside = int(raw_input("How many sides for each die?"))
return userdiceside
for rollval in range(1,userdicenum):
rollval = randint(1,userdiceside)
print int(rollval)
roll = roll + rollval
return roll
print roll
from random import randint
def diceroll():
def dicenum():
userdicenum = int(input("How many dice would you like to roll?"))
return userdicenum
def dicesides():
userdiceside = int(input("How many sides for each die?"))
return userdiceside
roll = 0
dicesida = dicesides() # so you dont have to re type it =)
for rollval in range(dicenum()):
rollval = randint(1,dicesida)
print(int(rollval))
roll = roll + rollval
return roll
print(diceroll())
is this what you want?
One difference between Python3 and Python2 is that in Python3, the print statement is a function in Python3, but a keyword in Python2. The fact that it is a function means that you have to use it like any other function, which is by putting the arguments within parenthesis.
Use print(int(rollval))
You should also have a look at the second line. roll == 0 should probably be roll = 0. And as mentioned in comments, you should also not use raw_input in Python3. Use input.
This question already has answers here:
Dynamic variable in Python [duplicate]
(2 answers)
Closed 8 years ago.
What im trying to do is have the user type a number, which would then register into the system as a specific variable.
Example:
n1 = "X"
n2 = "Y"
n3 = "Z"
num = input("enter a number (1-3): ")
print(n(num))
So, if the user entered the number 2 into their program, the program would display the value stored in n2, or be able to use n2 in an equasion.
Is this possible? I'm still new to Python and this is not a school assignment, just my own curiosity :)
Thanks
EDIT:
Here is what im trying to do:
temp = int(input("\nPlayer One, please pick a square (1-9): "))
while {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "X" or {1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] == "O":
temp = str(input("\nPlayer One, please pick a valid square (1-9): "));
{1:n1, 2:n2, 3:n3, 4:n4, 5:n5, 6:n6, 7:n7, 8:n8, 9:n9}[temp] = "X"
You could use a dictionary for this. Like:
num = input("...")
print {1:n1, 2:n2, 3:n3}[num]
Hope that helps.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am new to python and I am curious to why this error is occurring. This error only occurs when I input a number (selection) greater than 3. When the input is 1,2, or 3 it works as if it is supposed to
error:
File "/Users/username/Development/foodcalc/interface.py", line 12, in display_options
display_options()
NameError: global name 'display_options' is not defined
code
from interface import Interface
interface = Interface();
print "welcome"
print "what would you like to do?"
interface.display_options()
my interface.py
class Interface:
#options = ["1 - create a meal plan","2 - add food","3 - look up food"]
def display_options(self):
options = ["1 - create a meal plan","2 - add food","3 - look up food"]
for choice in options:
print choice
selection = int(raw_input())
if selection > 3:
print "Incorrect selection. Please try again."
display_options()
else:
if selection == 1:
print "meal"
elif selection == 2:
print "add"
else:
print "search"
When you attempt to call a member function or method you need to preface it with self.. Otherwise the interpreter looks for a global function with the name you've requested.
It is because you didn't use an instance to call the function. add self. in-front of the function.
As a side note; it would be better to implement this in a while loop seeing as a continuous entry of incorrect values will cause your program to crash due to reaching the recursion limit.
To prevent this bug, consider writing the function like this:
def display_options(self):
options = ["1 - create a meal plan","2 - add food","3 - look up food"]
selection = 0
while selection > len(options) or selection <= 0:
for choice in options:
print choice
try:
selection = int(raw_input())
if selection > len(options) or selection <= 0:
raise Exception()
except:
print "Incorrect selection. Please try again."
if selection == 1:
print "meal"
elif selection == 2:
print "add"
else:
print "search"