Simple Python

A forum for general discussion of the Python programming language.

Simple Python

Postby LCC » Wed Sep 28, 2016 9:30 pm

I have only just started studying computer science at A-Level and I have been set this task in python. I know it probably seems easy but I am very knew to this and am unsure of how to go about doing it please help. :)

Write a program that gets the user to type in how many bits
they are using and will then tell them what the value of the
left-most column heading will be in two's complement e.g. if
they type in 4 the program should output -8.

Thanks.
Last edited by micseydel on Wed Sep 28, 2016 9:31 pm, edited 1 time in total.
Reason: Initial post lock.
LCC
 
Posts: 2
Joined: Wed Sep 28, 2016 9:27 pm

Re: Simple Python

Postby micseydel » Wed Sep 28, 2016 9:34 pm

What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?
Due to the reasons discussed here we will be moving to python-forum.io on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
micseydel
 
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Simple Python

Postby LCC » Thu Sep 29, 2016 7:23 am

micseydel wrote:What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?



I have managed to get the input:

number = int(input("How many bits are you using?"))

I just cannot figure out how to use two's complement in my code to produce the correct output.
LCC
 
Posts: 2
Joined: Wed Sep 28, 2016 9:27 pm

Re: Simple Python

Postby Ofnuts » Fri Sep 30, 2016 11:31 am

LCC wrote:
micseydel wrote:What have you tried? If you're totally unsure, try to ask as specific a question as possible. Are you stuck with input, before doing anything with it? Does input work, but you're not sure how to compute 2's complement in Python?



I have managed to get the input:

number = int(input("How many bits are you using?"))

I just cannot figure out how to use two's complement in my code to produce the correct output.

You don't need to. if you have N bits you can store 2**N values. In two's complement, one half of that is positive numbers, from 0 inclusive to (2**(N-1)-1). How can you express the bounds of the other half in a similar way, and just have a small formula that uses N to compute the lower bound?
This forum has been moved to http://python-forum.io/. See you there.
User avatar
Ofnuts
 
Posts: 2659
Joined: Thu May 14, 2015 9:46 am
Location: Paris, France, EU, Earth, Solar system, Milky Way, Local Cluster, Universe #32987440940987


Return to General Discussions

Who is online

Users browsing this forum: Bing [Bot] and 2 guests