[Tutor] (no subject)

Liam Clarke ml.cyresse at gmail.com
Sun Sep 25 04:35:11 CEST 2005


I'd also like to mention that input() is bad, as you can enter Python
statements and it'll execute them. For instance, entering "sys.exit()"
would cause your programme to try and call sys.exit(), which if the
sys module had been imported, would quit.

You can do far more malevolent things than that, of course.

Best to use

g = int(raw_input("Enter a score number (0-100): "))

Come Python 3.0, raw_input and input are to both get the boot, I
believe, in favour of sys.stdin.read()
On 9/25/05, bob <bgailer at alum.rpi.edu> wrote:
>  At 03:29 PM 9/24/2005, Goofball223 at wmconnect.com wrote:
>
> Hello
>
>  How would I get the following program to accept inputs of exam scores from
> 0-100 with A being 100-90, B being 89-80, C being 79-70, D being 69-60, and
> F being everything less than 60?
>  Many solutions are available. One could use an if statement:
>
>  if g >= 90: s = 'A'
>  elif g >= 80: s = 'B'
>  ...
>  else: g = 'F'
>
>  although that can be hard to maintain.
>
>  Or associate the lower limit with the data and search using a loop:
>
>  scores = [("A",90), ("B",80), ("C".70), ("D",60),("F",0) ]
>  for letter, number in scores:
>      if g >= number:break
>  print "The score of your exam is", letter
>
>  This separates the data from the program structure and becomes much easier
> to maintain / extend, apply to new situation.
>
>  If you are using a database then you could store these value pairs in a
> table and use SQL to retrieve the desired letter.
>
>  When the number ranges have a nice progression, you can reduce the number
> to an index:
>
>  print "The score of your exam is", "FFFFFEDCBAA"[g/10] # this is simpler
> code but harder to read/maintain.
>
>  Or - knowing that chr(65) = "A", chr(66) = "B" you could convert the number
> to be in the range 65..70 and use chr()
>
>
> import string
>  You do not refer to the string module, so there is no need to import it.
> Also be ware that it will eventually go away.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>


More information about the Tutor mailing list