Errors when asking for user input of "byte"

This is the place for queries that don't fit in any of the other categories.

Errors when asking for user input of "byte"

Postby Jared.m21 » Thu Jul 17, 2014 12:13 pm

I am writing a script to convert amounts of one datatype to another. When converting bytes to anything else it always gets the wrong answer. If anyone could take a look at my script and let me know what you think that would help a lot.

Code: Select all
#gathering the variables of the orig unit, the amount,  and the unit converted to
print"Valid units are Bytes, Kilobytes, Megabytes, Gigabytes, and Terabytes"
unit = raw_input('First unit: ')
amt = input('Number of units: ')
con = raw_input('Unit converted into: ')

#convert units to uppercase for continuity, also my *fix* for the int 'byte'
unit = unit.upper()
con = con.upper()

#recognise units if they are typed out
if unit == 'BYTES':
    unit = 'b'
elif unit == 'KILOBYTES':
    unit = 'kb'
elif unit == 'MEGABYTES':
    unit = 'mb'
elif unit == 'GIGABYTES':
    unit = 'gb'
elif unit == 'TERABYTES':
    unit = 'tb'
elif unit == 'BYTE':
    unit = 'b'
elif unit == 'KILOBYTE':
    unit = 'kb'
elif unit == 'MEGABYTE':
    unit = 'mb'
elif unit == 'GIGABYTE':
    unit = 'gb'
elif unit == 'TERABYTE':
    unit = 'tb'

#recognise conversion units as they are typed
if con == 'BYTES':
    con ='b'
elif con == 'KILOBYTES':
    con = 'kb'
elif con == 'MEGABYTES':
    con = 'mb'
elif  con == 'GIGABYTES':
    con = 'gb'
elif con == 'TERABYTES':
    con = 'tb'
elif con == 'BYTE':
    con = 'b'
elif con == 'KILOBYTE':
    con = 'kb'
elif con == 'MEGABYTE':
    con = 'mb'
elif con == 'GIGABYTE':
    con = 'gb'
elif con == 'TERABYTE':
    con = 'tb'

#convert units to lowercase because I coded this beforehand
unit = unit.lower()
con = con.lower()

#this part does all of the calculations
if unit == 'b':
    if con == 'b':
        print(amt)
    elif con == 'kb':
        amt = amt / 1024
        print(amt)
    elif con == 'mb':
        amt = amt / 1024 / 1024
        print(amt)
    elif con == 'gb':
        amt = amt / 1024 / 1024 / 1024
        print(amt)
    elif con == 'tb':
        amt = amt / 1024 / 1024 / 1024 / 1024
        print(amt)
    else:
        print "invalid conversion unit"
       
elif unit == 'kb':
    if con == 'b':
        amt = amt * 1024
        print(amt)
    elif  con == 'kb':
        print(amt)
    elif con == 'mb':
        amt = amt / 1024
        print(amt)
    elif con == 'gb':
        amt = amt / 1024 / 1024
        print(amt)
    elif con == 'tb':
        amt = amt / 1024 / 1024 / 1024
        print(amt)
    else:
        print"invalid conversion unit"
       
elif unit == 'mb':
    if con == 'b':
        amt = amt * 1024 * 1024
        print(amt)
    elif con == 'kb':
        amt = amt * 1024
        print(amt)
    elif con == 'mb':
        print(amt)
    elif con == 'gb':
        amt = amt / 1024
        print(amt)
    elif con == 'tb':
        amt = amt / 1024 / 1024
        print(amt)
    else:
        print"invalid conversion unit"
       
elif unit == 'gb':
    if con == 'b':
        amt = amt * 1024 * 1024 * 1024
        print(amt)
    elif con == 'kb':
        amt = amt * 1024 * 1024
        print(amt)
    elif con == 'mb':
        amt = amt * 1024
        print(amt)
    elif con == 'gb':
        print(amt)
    elif con == 'tb':
        amt = amt / 1024
        print(amt)
    else:
        print"invalid conversion unit"
       
elif unit == 'tb':
    if con == 'b':
        amt = amt * 1024 * 1024 * 1024 * 1024
        print(amt)
    elif con == 'kb':
        amt = amt * 1024 * 1024 * 1024
        print(amt)
    elif con == 'mb':
        amt = amt * 1024 * 1024
        print(amt)
    elif con == 'gb':
        amt = amt * 1024
        print(amt)
    elif con == 'tb':
        print(amt)
    else:
        print"invalid conversion unit"
       
else:
    print"invalid starting unit"
Last edited by Mekire on Thu Jul 17, 2014 12:51 pm, edited 1 time in total.
Reason: First post lock.
Jared.m21
 
Posts: 1
Joined: Thu Jul 17, 2014 12:06 pm

Re: Errors when asking for user input of "byte"

Postby micseydel » Tue Jul 29, 2014 7:08 pm

I think the reason you haven't gotten a response to this is that your post is overly large. Try giving this a read, especially the parts about providing example input and output and providing the minimal code to reproduce your problem.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1391
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Errors when asking for user input of "byte"

Postby Kebap » Thu Jul 31, 2014 10:00 am

Jared.m21 wrote:When converting bytes to anything else it always gets the wrong answer. If anyone could take a look at my script and let me know what you think that would help a lot.

I assume you mean, for example, when you try to convert 123 bytes to kilobytes, you receive the answer 0. But it is really more than 0, only not enough for 1. To fix this problem in python 2, you can import the division function from python 3, where it works as expected. Just add the following line at the top of your code:

Code: Select all
from __future__ import division
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 396
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests

cron