4 Replies - 512 Views - Last Post: 16 April 2013 - 06:18 AM Rate Topic: -----

#1 sykoticm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 07-October 11

raw_input, printing, and user input questions...

Posted 30 March 2013 - 10:42 PM

Before i get to my questions I want to inform all who read this that either my browser (firfox) is acting up or there is something wrong with the website. I cannot for reasons unkown to me use the search function available at the top right of the website (also the advanced search link below it). So if this type of question has been post which i think it might have please direct me to the correct location.

Ok so i have some basic knowledge of programming, I took a course at my local vocational school. Long story short after one year of learning programming and dealing with an awful instructor i decided to leave the course. Now in that course i learned the basics of programming and most of the java syntax.

Now i have been actively trying to learn python for 2 days, i have learned some of the quirks of the syntax with variables, strings, integers, how raw_input is to be used instead of input, how to define functions, and how if, else, and elif statements work.

I don't know if anyone in this forum knows java but I'm hoping someone does, well I'm sure you can understand it enough to figure out what I'm trying to convey and answer the question.

import java.util.Scanner;

public class example {

     public static void main(String[] args){

Scanner sc = new Scanner(System.in);
String a;

System.out.println("enter a string");
a = sc.nextLine();

System.out.println (a);

}
}



Alright I'm not 100% sure that is correct so I'm going to explain it.

we import a scanner "module"

next we create the main class

we set the scanner module to work through "sc"

we create a String called a

we print a message telling the user to type a string

the a = sc.nextLine(); gathers the user input and stores it in the a variable

then the program prints the String a



Now in python as far as I'm aware the only way to get user input is the following:


a = raw_input("enter a string")

print (a)




Now at the beginning of the program the a variable is set to "enter a string" **(is that correct?)** and then immediately displays "enter a string" and then the user must input a string or just hit enter and the program will print the string.

So am I trying to think in java syntax to program in python? Because it seems odd that as far as I know you cant print text and then ask the user for input gather the input and assign it to a variable. The way I understand python to work right now is that you cannot create a variable without immediately asking for input or manual assigning a value. So to do what I feel comfortable with or how I currently understand programming **(since I started with java)** I would have to do the following:

 

print ("enter a string")

a = raw_input ("")

print (a)




So then if that is the only way to make it work or look like how i accomplished getting user input in java since It cannot be used outside of the defined function. This doesn't work:



def start():
    print ("enter a string")
    a = raw_input("")

def Print():
    print (a)

start()

Print()





So is there a way I can get this to work or am I looking at it all wrong, have I yet to learn something in the python syntax that I'll be learning when you guys reply saying I'm a doofus?? :)/>

Is This A Good Question/Topic? 0
  • +

Replies To: raw_input, printing, and user input questions...

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 116
  • View blog
  • Posts: 212
  • Joined: 11-January 13

Re: raw_input, printing, and user input questions...

Posted 30 March 2013 - 11:19 PM

Well, I don't really know Java, but nonetheless I guarantee that the way you are trying to justify things is grossly overcomplicating them.

raw_input is a function. It will print the string argument it is passed and then wait for a user input. The string argument you pass it is not part of the return value.

a = raw_input("Input whatever: ")
will assign the return value of the raw_input function to a. a couldn't care less what string you passed to raw_input.

As for your functions you wrote at the end, they don't work because variables defined in functions are local. You need to return values from functions.

Pretty much the way you did it originally is probably what you want though:
a = raw_input("Enter a string: ") 
print (a)
a now holds the value of the raw_input (what the user typed; not the string you passed to raw_input) and you also printed a to the screen (which seems redundant as the user just typed it). What about this isn't what you want?

-Mek
Was This Post Helpful? 0
  • +
  • -

#3 sykoticm  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 28
  • Joined: 07-October 11

Re: raw_input, printing, and user input questions...

Posted 30 March 2013 - 11:31 PM

View PostMekire, on 31 March 2013 - 12:19 AM, said:

Well, I don't really know Java, but nonetheless I guarantee that the way you are trying to justify things is grossly overcomplicating them.

raw_input is a function. It will print the string argument it is passed and then wait for a user input. The string argument you pass it is not part of the return value.

a = raw_input("Input whatever: ")
will assign the return value of the raw_input function to a. a couldn't care less what string you passed to raw_input.

As for your functions you wrote at the end, they don't work because variables defined in functions are local. You need to return values from functions.

Pretty much the way you did it originally is probably what you want though:
a = raw_input("Enter a string: ") 
print (a)
a now holds the value of the raw_input (what the user typed; not the string you passed to raw_input) and you also printed a to the screen (which seems redundant as the user just typed it). What about this isn't what you want?

-Mek


I seem to over complicate lots of things

It isn't what I want I'm just trying to understand the syntax... So now that you have confirmed that the variable is local.... how do I make a variable that isn't local that I could use in another defined function? Could do something like this:


#a would be a local variable (i know this is wrong its just an example)
String a

b = raw_input("enter a string")

a = b

print(B)/>/>/>




I don't know what the "/>" is about after
print(B)/>/>
i have deleted it several times in the edit option and then re-posted but it keeps appearing.

Does that make sense?


ok this is weird and annoying /> keeps showing up in my posts and i don't know why after deleting them and submitting the modified post they still exist

This post has been edited by sykoticm: 30 March 2013 - 11:40 PM

Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: raw_input, printing, and user input questions...

Posted 16 April 2013 - 06:06 AM

If you know Java then you know how to pass an argument to a function.
If you want your 'Print()' function to print something then pass it something to print.
Like this.

def Print(theStringToPrint):
    print(theStringToPrint)

def DoublePrint(theStringToPrint):
    print(theStringToPrint + ' : ' + theStringToPrint)

letsPrintThis = 'hello world'

Print(letsPrintThis)

DoublePrint(letsPrintThis)



Nothing in any way different to the ideas from Java in that code is there?
Functions accept arguments and do work on the arguments they are passed.
That must seem familiar, surely?
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,019
  • Joined: 12-December 12

Re: raw_input, printing, and user input questions...

Posted 16 April 2013 - 06:18 AM

I wouldn't create a function named Print() - it is a recipe for confusion and future errors.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1