How do functions work? [closed] - python

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 5 years ago.
Improve this question
Except for in maths examples, of which there are many, I don't understand functions properly.
What is wrong with this?
def get_address():
address = raw_input ("What is your address: ")
return address
I'm not able to get a variable address returned for use later.

What you did, you failed to call the function that is why you facing this problem.
Use this instead and you can able to get the address of a function.
In Python v2.x
#!/usr/bin/python
def get_address():
address = raw_input("What is your address: ")
return address
a = get_address()
print a
What is raw_input?
It ask the user (the optional arg of raw_input([arg])), gets input from the user and returns the data input by the user in a string.
In Python v3.x:
#!/usr/bin/python
name=input('Enter your name : ')
print ("Welcome %s, Let us be friends!" % name);

Related

How do i fix this function in python? Im the definition of a noob at python, still learning, so please help. Its suppposed to ask for the name and age [closed]

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
def say_hi(x, y):
name = x
age = y
print("Hello " + name + ". You are " + age + " years old. Is this correct?" )
x = input("Enter your name: ")
y = input("Enter your age: ")
say_hi()
#i know the answer to this question is probably simple as hell but please help, i really want to get good at coding and make my own programs.
There's a few things your code is missing.
In Python, program flow is controlled by using indentation. The contents of the function should be indented (using tabs or spaces at the start of each line), so that it's clear that those lines of code belong to the function say_hi().
When calling a function, the variables that it uses should be passed to it inside the parentheses, like this: say_hi("A name", "an age").
You may also want to use meaningful variable names in your function definitions, so that it's clear what each parameter is supposed to be used for
Here's some improved code with comments explaining what I've done
# Function definition contains clear variable names
# Code inside the function is indented correctly
def say_hi(name, age):
print("Hello " + name + ". You are " + age + " years old. Is this correct?" )
# Outside the function definition, we get the user's input
x = input("Enter your name: ")
y = input("Enter your age: ")
# And when we call the function, we give it the
# variables so that Python knows what we want to use as arguments
say_hi(x, y)
I hope this helps! I'd highly recommend looking at some beginner's programming courses on YouTube, since seeing some real-world examples will hopefully help you along a lot with writing good code. If you're not already, you may want to try out a code editor (I use VS Code, but there's tons of options), which will help you find simple errors like these more easily by putting red underlines where it thinks there's an error (much like a document editor does for spelling). Best of luck with programming in the future!
You need to pass x and y into the function like this -
say_hi(x,y)

Assigning input to a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying a simple code but I'm stuck. I want to ask a question to the user and store the answer to an input, and then write it on a file.
Here's the code :
input = open("Wishes.txt", "w")
wish = input("What do you wish ?")
input.write(wish)
print("Thank you")
I get this error : TypeError: '_io.TextIOWrapper' object is not callable
I'm sure it is really easy but I'm a beginner so I don't know where to find a solution. Thank you in advance.
You have hidden the function name input by creating a variable called input. Simply rename the variable.
Also you probably want to append to the file instead of overwriting the whole thing. And you should be using with to properly handle an external resource.
wish = input("What do you wish ?")
with open("Wishes.txt", "a") as file:
file.write(wish + "\n")
print("Thank you")

Input function doesn't read the next line of code [closed]

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 2 years ago.
Improve this question
I'm trying to learn how the input function works but for some reason it doesn't want to read any code that's after it.
Here's my code:
f_name = input("enter name: ")
print("welcome", f_name)
and this is the result:
enter name: This is my name
and nothing else comes after I hit enter.
For python 3.x compiler, this works fine. I guess what you can do is, print your data in one of the following ways, and see, if that works for you.
Taking f_name as UserName
1. Using f-String
>>> print(f"Welcome {f_name}.")
Welcome UserName.
2. String concatenation
>>> print('Welcome ' + f_name)
Welcome UserName
3. Using old school % formatting
>>> print('Welcome %s.' % f_name)
Welcome UserName.
4. Using .format()
>>> print('Welcome {}.'.format(f_name))
Welcome UserName.
Using these you can get the output. The code which you have pointed works out for me, although the above solutions is what gives the output surely. Give it a go, and let us know. :)
if it automatically closes, because nothing to execute after that so it closes try this to see the output
f_name = input("enter name: ")
print("welcome", f_name)
input()
For some reason running it with the default Python editor worked for me.
Sublime Text Doesn't work with input for some reason nor running it with cmd worked too.

Python variable does not contain any data [closed]

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 3 years ago.
Improve this question
I'm hoping this is a simple stupid problem. But we have an in-house program that is no longer working. The program is not able to pass the data which is supposed to be contained in a variable using var_name+="".
The variable seemingly contains nothing. when I try to print the contents of this variable to either a string or text doc, we get nothing. The variable in question here is "allData".
The contents of this variable need to be passed to our fax appliance.
def sendFax(destOrg, destFax, cliName, casenum, attachments, errEAddr, comment, destName):
creds=requests.auth.HTTPBasicAuth(user,password)
#OVERRIDE OUTBOUND FAX NUMBER FOR TESTING
destFax='716-631-9804'
print("faxes will be sent to "+destFax)
return
allData=''
allData+='<schedule_fax>\n'
allData+='<cover_page>\n'
allData+='<url>'+prepXMLString(coverPage)+'</url>\n'
allData+='<enabled>true</enabled>\n'
allData+='<subject>'+prepXMLString(cliName)+' - case # '+str(casenum)+'</subject>\n'
allData+='<comments>'+prepXMLString(comment)+'</comments>\n'
allData+='</cover_page>\n'
allData+='<sender>\n'
allData+='<name>'+prepXMLString(webAddr)+'</name>\n'
allData+='<organization>'+prepXMLString(ourOrg)+'</organization>\n'
allData+='<phone_number>'+prepXMLString(ourPhonenum)+'</phone_number>\n'
allData+='<fax_number>'+prepXMLString(ourFaxnum)+'</fax_number>\n'
allData+='<email_address>'+prepXMLString(errEAddr)+'</email_address>\n'
allData+='</sender>\n'
allData+='<recipient>\n'
allData+='<name>'+prepXMLString(destName)+'</name>\n'
allData+='<organization>'+prepXMLString(destOrg)+'</organization>\n'
allData+='<fax_number>'+destFax+'</fax_number>\n'
allData+='</recipient>\n'
That is all screwed up. You're returning on the line with the return keyword which is why nothing is being returned.
You should do all of the concatenation and then return.

Code to accept 32byte hex password [closed]

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 5 years ago.
Improve this question
I'm doing some experiment with the following code:
import hashlib
password = 16
n = 2 #counter
hash = hashlib.sha256(str(password) + str(n)).hexdigest() #will read the password as string and hash
print hash
What should be the proper code so it can accept a 32byte hex like 000000...0002?
A hex in Python is just a fancy way of writing an integer. So simply set password to your hex value:
password = 0x00000000000000000000000000000002

Categories