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.
Related
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)
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 1 year ago.
Improve this question
Why am I getting the following error code when I run this code:
builtins.NameError: name 'string' is not defined
def explore_string():
get_input()
explore_chars(string)
sum_digits(string)
def explore_chars(string):
print("Original: ",string)
print("Length: ",len(string),"chars")
print("2nd char: ",string[1])
print("2nd last: ",string[2])
print("Switched: ",string[-3:]+string[3:-3]+string[0:3])
def sum_digits(string):
dig_sum=0
l=['1','2','3','4','5','6','7','8','9']
for i in string:
if i in l:
dig_sum+=int(i)
print("Digit sum: ",dig_sum)
def get_input():
string=input("Enter 10 or more chars ending with a period: \n-> ")
while(len(string)<10 or string[len(string)-1]!='.'):
string=input("-> Error! Try again: ")
return string
explore_string()
You need to change your explore_string like this:
def explore_string():
string = get_input()
explore_chars(string)
sum_digits(string)
The result value of get_input() should be stored in a variable string
Imentu's answer seems like the right solution. However, I want to add some minor tips that could help you to solve such problems by yourself. Because you might encounter them many more times in the future (at least I did).
The error code oftentimes contains a lot of information about the problem that you're facing. In your case the xxx is not defined means that you are referencing an object xxx which Python doesn't know about yet (it is not defined).
Whenever you encounter this, then there are 2 main things you should check
Did I forget to assign it? (which is the case here)
Did I make a typo? (e.g. if you typed strng = get_input())
If you combine this with the fact that the issue is centered around the word "string" and the approximate line number given by the error, then you should be able to find the issue.
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);
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 6 years ago.
Improve this question
How can we store the output of 'type' function in a variable ?
next = raw_input('> ')
type = type(next)
if type == 'int':
val = int(next)
else:
print "Type a number!"
Syntax Error at line 4....?
There are several ways of doing what you want. Note that defining a type variable to mask the type function is not good practice! (and next either BTW :))
n = raw_input('> ') # (or input in python 3)
try:
val = int(n)
except ValueError:
print("Type a number!")
or
n = raw_input('> ') # (or input in python 3)
if n.isdigit():
val = int(n)
else:
print("Type a number!")
Note: as some comment indicated, that in python 2, it was possible to get what you wanted by just using
n = input("> ")
but very ill adviced since you have to control what n is really, not python 3 portable, and has huge security issues:
Ex: in python 2 on windows, try that:
import os
n = input("> ")
and type os.system("notepad")
you'll get a nice notepad windows !! You see that it is really not recommended to use input (imagine I type os.system("del <root of your system>")) ...
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
the python idle is throwing a error at the print() function im not sure why heres the code..
password = "cow"
name = input()
input("MR PENGUIN: hello there i am Mr Penguin what is your name? ")
input("well, hello there"+name+"Tell me your password")
input("You: my password is, ")
input("MR PENGUIN: im little defh could you repeat that? ")
input("YOU: my password is, "
print("PC POLICE: STOP! dont ever trust penguins with your data becuase he just told every one that your password is "+ password)
input("Press Enter To Exit")
You are missing a parenthesis at the end of the input on the prior line.
Change:
input("YOU: my password is, "
to:
input("YOU: my password is, ")
For the record, your print was fine. Note that when you get a cryptic error, it is often something on the previous line.
This is because your input statement in the previous line is missing a closing paranthesis.
It should be:
input("YOU: my password is, ")
instead of
input("YOU: my password is, "