Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
* * * * * 1 votes

Refreshing In Python: Using Strings / Variables - Problems?

string

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 18 November 2012 - 03:12 PM

So I am trying to accomplish an efficient way to assign an integer to a variable or a string, then check true or false with an integer value of the variable or string as an option to choose from as user input. Whereas else or != to the variable / string declares a syntax error for the user.. Here are some example codes I have created myself in order to refresh myself in the process. I am looking for some pointers, it has been a good year or so since I have sat down and really been fluently programming. So, I am refreshing myself among the many languages we have here to learn from starting with Python as you can see.

I am running Python (IDLE) 2.5.4


Here are my examples...

Simple Banking Program In Python [CODEBOX #1]
def main():
print "############################ Python Internation Bank #########################"
print "	 [A] Open An Account"
print "	 [B] Access Account"
print "##############################################################################"
def userInput():
getinput = raw_input("Test >>> ")
if getinput == 0:
	 ### Open Account Script ###
	 print "Open Account"
if getinput == 1:
	 ### Access Account ###
	 print "Access Account"
		
else:
	 main()
return
main()



Simple Guest Book In Python [CODEBOX #2]
## Declaring strings as raw_input to get information from the user. ##
## Guest Book 1.0 ##
0 = "y", "Y"
1 = "n", "N"
def valfunc(): ## Validation using text_strings as user input?? Needs Work ##
							 ## String Error: Syntax ##
	 validation = raw_input('Is this correct? <Y/N>: ')
	 if validation == 0
		 print "CORRECT NAME"
	 if validation == 1
		 print "INCORRECT NAME"
		 main()
	 if validation != 0, 1;
		 print "Else Statement: Incorrect Syntax Error"
		 valfunc()
	 return
def mainfunc():
print "Guest Book Check-in\n";
LastN = raw_input('What is your last name?: ')
FirstN = raw_input('Thank you, now please type your first name?: ')
print "Thank you for signing into the guest book. Your name is " + LastN + ", " + FirstN
valfunc()
return
mainfunc()


Now, I see that this code is unfinished, but due to the fact that I am not just trying to refresh myself in programming. I have been programming on and off for a few years now. I am studying Computer Science and teaching myself "efficiency" in programming. So, I have an outline done and a flow chart that I am using to outline each one of my programs.

1. ) I need to know an efficient way to assign these variables to the integers. [A, a] = 1 [B, b] = 0 [N, n] = 1 [Y,y] = 0

Now I was certain I was past variables, strings, and integers.. But, it seems I am missing something in syntax with python that I am not doing right.

2. ) I am using these variables as a method to have Options in a text menu, as well as to get data, assign it, and check it.

Is there a more efficient way to do this process? Any pointers are appreciated.

Update: I have tried declaring them as global variables outside of my functions. One problem I am facing is that I am getting stuck in an infinite loop with [CODEBOX #2] on line

Line 14;
print "Else Statement: Incorrect Syntax Error"

I have tried various other structures for my variables. But, Python doesn't support case or classes so I have to use if statements. My code might be a little rough to follow due to the fact I was deconstructing and constructing it a few times over, but I think I kept it pretty cleaned up from all the changing and rearranging I did. I hope you all follow what I am saying and what I am focussing on accomplishing with this task. Thanks again for reading guys!

Thanks guys! Glad to be back,
Donovan

#2 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts

Posted 18 November 2012 - 07:54 PM

I am no Python programmer and honestly can't remember any of it. But from what I do remember, it does look like there is a possible syntax error on this line:
if validation != 0, 1;
				 print "Else Statement: Incorrect Syntax Error"
				 valfunc()

Python doesn't require ';' to end lines and also uses indentation (this is what leads me to these lines). It's the fact that you've actually added a ';' instead of ':'. And the loop comes from the fact that the code becomes:
def valfunc(): ## Validation using text_strings as user input?? Needs Work ##
														 ## String Error: Syntax ##
		 validation = raw_input('Is this correct? <Y/N>: ')
		 if validation == 0
				 print "CORRECT NAME"
		 if validation == 1
				 print "INCORRECT NAME"
				 main()
		 if validation != 0, 1;
		 print "Else Statement: Incorrect Syntax Error"
		 valfunc()
		 return

Which means now if validation != 1, you will get an infinite loop on:
print "Else Statement: Incorrect Syntax Error"

Since main the main function will always be calling valfunc() (see my EDIT below).

As for where to assign them variables, I'm not to sure what you're trying to accomplish and so can't really help at the moment. What are you going to do with the variables?


On the note of 'efficient' programming (I'll write a blog about it later), there is many ways to accomplish this. Where you assign some variables doesn't matter too much in this case. In more complex programs, you'll have to be very careful with it. And what I would define as an efficient program all depends on the task you're trying to accomplish including the flexibility and scalability of the code.

You can measure it using numerous ways though and I'm unsure if you've came across it in your CS degree just yet but in my SE degree, we don't do it till third year :/ Which I believe is a bit late, you should learn it quite early since it isn't overly complex (I did it in my second year). Measuring your code using big-oh notation will be extremely helpful (that will determine the efficiency). As for the flexibility and scalability, it's all about using the correct software design patterns when appropriate as well as, and I've read this numerous times since I've been back so good to see that many agree, decoupling your code.

Will explain more in my blog later this week :)

Good to see you back and taking on some old programming languages :)

EDIT: Just re-reading my comments and your code, it will not loop because the main function will call valfunc but because the if statement is suppose to do, since there is a semicolon, valfunc will run at the end of every call to valfunc, causing an infinite loop. Why the print statement is infinitely loop still holds from what I said earlier.
I've returned...

#3 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 18 November 2012 - 08:44 PM

Some interesting points you made about efficiency and flexibility within programming patterns as well as throwing in the big-oh notation. I will read up into those sometime shortly to brush up.

As for what I am trying to accomplish. I am trying to get inputs and outputs like this.



Guest Book Check-In

What is your last name:? Doe
Thank you, now please type your first name?: John
Thank you for signing into the guest book. Your name is Doe, John



Currently, I have the above all working properly of course. Below is where I get problems.

What it does...


Is this correct? <Y/N>: n
Else Statement: Incorrect Syntax Error

Is this correct? <Y/N>: N
Else Statement: Incorrect Syntax Error

Is this correct? <Y/N>: 1
Else Statement: Incorrect Syntax Error

Is this correct? <Y/N>: y
Else Statement: Incorrect Syntax Error

Is this correct? <Y/N>: Y
Else Statement: Incorrect Syntax Error

Is this correct? <Y/N>: 0
Else Statement: Incorrect Syntax Error


Is this correct? <Y/N>: <Anything Else The User Inputs>
Else Statement: Incorrect Syntax Error



What it should do..

Is this correct? <Y/N>: n
INCORRECT NAME

Is this correct? <Y/N>: N
INCORRECT NAME

Is this correct? <Y/N>: 1
INCORRECT NAME

Is this correct? <Y/N>: y
CORRECT NAME

Is this correct? <Y/N>: Y
CORRECT NAME

Is this correct? <Y/N>: 0
CORRECT NAME

Is this correct? <Y/N>: <Anything Else The User Inputs>
Else Statement: Incorrect Syntax Error



#4 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 19 November 2012 - 01:09 AM

Did you mean that?
A, a = 1
Variables A and a have values of 1.

It's important to note that raw_input returns strings, so you can't compare them against integers.
user_input = raw_input("Enter y or n: ")
if user_input == "y":
    print "You selected yes"
else if user_input == "n":
    print "You selected no"
else:
    print "Invalid choice"

The roots of education are bitter, but the fruit is sweet.


#5 Donovan

Donovan

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 633 posts

Posted 20 November 2012 - 05:14 PM

Did you mean that?

A, a = 1
Variables A and a have values of 1.

It's important to note that raw_input returns strings, so you can't compare them against integers.
user_input = raw_input("Enter y or n: ")
if user_input == "y":
print "You selected yes"
else if user_input == "n":
print "You selected no"
else:
print "Invalid choice"

Did you mean that?

A, a = 1
Variables A and a have values of 1.

It's important to note that raw_input returns strings, so you can't compare them against integers.
user_input = raw_input("Enter y or n: ")
if user_input == "y":
print "You selected yes"
else if user_input == "n":
print "You selected no"
else:
print "Invalid choice"


Thank you for posting that example Flying Dutchman! I have added to your reputation for contributing to the thread.

So, I am defining my functions and global variables correctly? I just can't use them with raw_input, correct? and what is the difference between defining user_input as a function instead of assigning it to raw_input?

Thanks, for your replies guys,
Donovan

#6 Flying Dutchman

Flying Dutchman

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1090 posts

Posted 20 November 2012 - 06:13 PM

If you're referring to your valfunc function, then yes. But before you exit that function, you're calling it again (recursion) and because there is no stop condition, it causes stack overflow and it crashes.

user_input, in my case, is a variable, not a function, that holds whatever the user has typed in. You could create another name for function (super easy in Python, a bit more complex in some other languages). For example:
get_input_from_stdin = raw_input
name = get_input_from_stdin("What's your name? ")

If you typed that into Python shell:
>>> get_input_from_stdin = raw_input
>>> name = get_input_from_stdin("What's your name? ")
What's your name? Flying Dutchman
>>> name
'Flying Dutchman'
>>> get_input_from_stdin
<built-in function raw_input>
you can see it says <built-in function raw_input> for get_input_from_stdin (you have to type it without parenthesis).

You could create a function user_input that would ask for something and then return that value. Difference between that and calling raw_input by itself are none, except if you'd do some error checking in your function, then it would just result in nicer/cleaner code structure.

The roots of education are bitter, but the fruit is sweet.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download