Help writing a Python program (Python 3.2 for Windows)

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Sun Sep 04, 2011 5:52 pm UTC

I'm writing a program in Python that will calculate the users class average for a single class. Right now, it just uses the Python shell to ask for the users input because I haven't yet learned GUI's.

When I'm done, it'll ask the user to input the number of tests/quizzes/hw problems for the year and then ask them for how much of the final grade they are worth. Then, it'll ask the user to input each grade.

But I've hit a snag.

I'm using a while loop so the program stops when the number of grades entered equals the number the user input for how many grades there were.
Basically, something like this:
Code: Select all
test = int(input("Total number of tests for the year? "))
testTotal = 0
count = 0
while count < test:
    user = int(input("Enter grade: "))
    count += 1
    testTotal += user

testAverage = testTotal / test
print(testAverage)


I've got some other stuff in there to (validation loops, etc). This is just the most basic version of the code.

The problem is, the output looks like this:
Code: Select all
Total number of tests for the year?
Enter grade:
Enter grade:
Enter grade:
(repeat until all grades have been entered)


I want it to print:
Enter grade 1:
Enter grade 2:
Enter grade 3:
etc.

How do I do this?


EDIT:
I would also like to be able to ask the user a Y/N question.
I want it to ask the user if they want to see their average for each individual section (test, quiz, homework, etc) and, if they input "Y", have it print those to the screen.
How would I implement Y/N prompts in Python?
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby snoose » Sun Sep 04, 2011 6:45 pm UTC

You need to use string formatting.
Code: Select all
user = int(input("Enter grade %d: " % count))


http://docs.python.org/library/stdtypes.html#string-formatting-operations
snoose
 
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Sun Sep 04, 2011 6:51 pm UTC

Thank you! (And thanks for the link)
It works now.


But what about my second question?
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby snoose » Sun Sep 04, 2011 7:05 pm UTC

Decide what you will except as a 'yes' (yes, y, YeS, ... (probably good to ignore case, therefore YeS = yes)) and just look for that in the user's input, everything else is treated as no.
snoose
 
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Sun Sep 04, 2011 7:13 pm UTC

snoose wrote:Decide what you will except as a 'yes' (yes, y, YeS, ... (probably good to ignore case, therefore YeS = yes)) and just look for that in the user's input, everything else is treated as no.


I want them to just have to enter a capital Y or capital N.

Code: Select all
user = input("Do you wish to see your average? Y/N? ")
if user == Y:
     print(testAverage)

^Doesn't work. It give me the error "name 'Y' is not defined".
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby snoose » Sun Sep 04, 2011 7:46 pm UTC

There are 2 issues with your code.

1) input() returns the type of input that is entered at the prompt. If you want the user to enter a string they would have to enter "Y" (with the quotes). This is not obvious to the user, therefore you will want to use raw_input() instead because the returned type is always a string.

2) Take another look at your comparison. You are not doing a string comparison. It should be:
Code: Select all
if user == "Y":


Note: Both of these issues would result in the same error message you are seeing, after you fixed the 1st you would see the same error on the next line. You never told the interpreter what Y is. Obviously you didn't intend to use Y in the way you did, just pointing it out for future encounters.
snoose
 
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Sun Sep 04, 2011 8:01 pm UTC

raw_input() doesn't work. It keeps saying "name 'raw_input' is not defined".

However, putting the quotes around Y in the if statement did.
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby snoose » Sun Sep 04, 2011 8:12 pm UTC

My apologies for any confusion I may have caused. It looks like input() in Python 3 has been changed so raw_input() is no longer required and therefore no longer exists. I haven't use Python in a few years and never upgraded my knowledge to version 3. ;)
snoose
 
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Sun Sep 04, 2011 8:14 pm UTC

That's fine.

Thank you for the help. :D
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Jof16's » Mon Sep 05, 2011 2:37 pm UTC

I'm going to make a new post to bump this thread and ask a new question.

How do I get the code to restart?
I'm writing a validation loop for the entire code. I need to be able to go back to the beginning of the code if a certain set of conditions is not met.
Is there any way to do this without using a while loop?


EDIT: Also, out of curiosity, what is the best language for programming smartphone apps. I'm specifically thinking of apps for Android and iOS.
Jof16's
 
Posts: 84
Joined: Mon Jun 14, 2010 4:08 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby cemper93 » Mon Sep 05, 2011 4:17 pm UTC

Getting back to the start of your code isn't really possible without a while loop. This syntax:
Code: Select all
while True: #validation loop
    # code here
    stuff = input()
    if not validate(stuff):
        continue

is what I would use.

The language you need to learn to program smartphone apps depends on the platform (Objective C for iOS, Java for Android). But I would suggest learning Python so well that you have a certain skillset first, because 1) programming (and debugging) on the PC is easier than on a smartphone and 2) learning Java or even Objective-C will be far easier if you know another language well first.
Hello good friend.<br>Thank you for the kind insight. To gain free XBOX, please click this link <a href="http://forums.xkcd.com/viewtopic.php?f=53&t=37971">for m&ouml;re information visit our website.</a><br>Also more can be found.<br>Best regard
User avatar
cemper93
 
Posts: 181
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Anonymously Famous » Mon Sep 05, 2011 7:39 pm UTC

You can make very basic, very limited Android apps with Google's App Inventor (Google it). It's really limited, but they're about to open source it, so it may make some leaps in coming months/years.

In addition to Java/Objective C, you're probably also going to want to familiarize yourself with XML.

Of course, you could also make a web app that would work with a smartphone's browser. Then you can use your scripting language of choice (for example, Python with Django, PHP, ASP.net, etc.) and a server that supports it.

As for your "restart" question: A loop would be the best way. There might be some other way, but I haven't gone too far into the depths of the Python world. I know that some languages have a label/goto structure, but it's functionally equivalent to loops and usually makes your code harder to read.
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby thoughtfully » Tue Sep 06, 2011 12:34 am UTC

You can also use anything that will run in the JVM, I believe. Including Jython (Python for the JVM), but also Groovy, Clojure, Scala, and the other Usual Suspects. See also http://developer.android.com/, but there are a few threads on the forums also, search the Coding forum for "android". You will lose some portion of the support from the Google SDK (library support for things like convenient access to the phone's features or Google services, etc.); I'm not sure how that works, exactly. I intend to play with it some day, but it's down the priority list a bit.

For that matter, it is possible to compile to native code using C/C++ and a cross-compiler, but that really isn't recommended for beginners, or indeed anyone who hasn't got some fairly specific need.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2065
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Anonymously Famous » Tue Sep 06, 2011 5:48 pm UTC

Thoughtfully, do you happen to have any links to tutorials or examples of other JVM languages being used to program for Android? I'm specifically looking for Jython, but I'm having trouble finding anything for any language but Java itself.

Thanks in advance.
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby thoughtfully » Tue Sep 06, 2011 8:08 pm UTC

I don't, but anything generic for Jython should work; you just won't get any of the Android/phone features. I actually don't know much about Java or the JVM, other than how to run the end product, and not in an Android context.
Image
Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.
-- Antoine de Saint-Exupery
User avatar
thoughtfully
 
Posts: 2065
Joined: Thu Nov 01, 2007 12:25 am UTC
Location: Minneapolis, MN

Re: Help writing a Python program (Python 3.2 for Windows)

Postby cemper93 » Wed Sep 07, 2011 12:49 pm UTC

Anonymously Famous wrote:Thoughtfully, do you happen to have any links to tutorials or examples of other JVM languages being used to program for Android? I'm specifically looking for Jython, but I'm having trouble finding anything for any language but Java itself.

Actually, the 7th google hit for "android python" is exactly what you need.

Sorry for my false information beforehand, I hadn't known that this existed.

I don't, but anything generic for Jython should work; you just won't get any of the Android/phone features.

That is false. In fact, it is more the other way round: You get (nearly) full access to the Android library, but can't use many standard library functions due to Android's restrictive security policies.
Hello good friend.<br>Thank you for the kind insight. To gain free XBOX, please click this link <a href="http://forums.xkcd.com/viewtopic.php?f=53&t=37971">for m&ouml;re information visit our website.</a><br>Also more can be found.<br>Best regard
User avatar
cemper93
 
Posts: 181
Joined: Sun Feb 20, 2011 2:35 pm UTC
Location: `pwd`

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Anonymously Famous » Wed Sep 07, 2011 2:34 pm UTC

cemper93 wrote:Actually, the 7th google hit for "android python" is exactly what you need.

Sorry for my false information beforehand, I hadn't known that this existed.


I'd seen scripting for Python before, and it does indeed look nifty, but it's not exactly what I was asking for. That will let me use Python (and other scripting languages) on my Android device if I have that app installed. I want to program an app in Jython (or any other language that runs on the JVM) and turn it into an Android app that anyone can run. It probably can be done, with a bit of work and a lot of workarounds, but it doesn't look like people have done it.
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Steax » Wed Sep 07, 2011 3:08 pm UTC

iOS is a complicated beast to tame, and you'll need to join the developer program ($99 a year iirc) and do it in Objective-C. There are code converters and such stuff as well, but they're not foolproof. Additionally, I suggest learning the basics on interfaces and designing mobile applications first, before actually writing code - I've seen too many talented programmers jump headfirst into the mobile app space and fail terribly due to design problems.
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 3029
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Anonymously Famous » Wed Sep 07, 2011 7:54 pm UTC

You can register for free to start using Xcode and an iPhone emulator to practice. I only know this because of a class I took, where we used Xcode, etc. to learn how to translate iPhone apps. But I think that if you want your apps to appear on the store, you'll have to pay the fee that Steax mentioned.
Anonymously Famous
 
Posts: 240
Joined: Thu Nov 18, 2010 4:01 am UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Steax » Thu Sep 08, 2011 1:29 am UTC

That's true. On the other hand, though, I recall the freely available version of xcode being less advanced than the one offered at the developer program. And since it requires you to learn their model and language, might as well go the whole way (no real point in just trying to make an iOS app that runs in a simulators, all while learning so much stuff).
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 3029
Joined: Sat Jan 12, 2008 12:18 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby snoose » Thu Sep 08, 2011 3:10 am UTC

Xcode is free and fully functional. If you choose to enroll as a developer ($99 - $299 USD) you gain support, the ability to distribute your software, and the ability to test on a physical device. If you are just beginning there is no real point enrolling as a developer. If at some point you decide you actually have something that you might be able to sell, then enroll, but there is no real reason to enroll before you get to that point. The simulator is fine for testing and if you really need to there are 'unofficial' ways to test it on the hardware.

iOS Developer Benefits
http://developer.apple.com/programs/ios/distribute.html#compare
snoose
 
Posts: 15
Joined: Thu Jan 07, 2010 10:04 pm UTC

Re: Help writing a Python program (Python 3.2 for Windows)

Postby Steax » Thu Sep 08, 2011 4:16 am UTC

Ah, the page on xcode saying one needs to be enrolled in the dev program got me. I didn't notice the "you can download for free anyway, from lion" beneath it.
In Minecraft, I use the username Rirez.
User avatar
Steax
SecondTalon's Goon Squad
 
Posts: 3029
Joined: Sat Jan 12, 2008 12:18 pm UTC


Return to Coding

Who is online

Users browsing this forum: No registered users and 11 guests