ok well this is my first thread that im asking 2 questions, im new here, these are from my high school college computer science class, one is newer and one is older, i went back to the older one because I still want to finish it and try and extend my knownledge of python programming, I also have a university computer science class that im working with java in that one but i do have a problem with that one that is sort of the same as this old i will post but back to this.

here is the first one (its the old one):

this program is to try and guess the computers number it has randomly generated and to print the random number and to say that either guess1 or guess2 was closer,

import random
x= random.randint(1,100)
print "I have chosen a number between 1 and 100. Try to guess it!"
guess1 = int(raw_input ("Please enter in your first guess:"))
guess2 = int(raw_input ("Now please enter in your second guess:"))
print "Guess#1:",guess1
print "Guess#2:",guess2
distance1 = x-guess1
distance2 = x-guess2
if (guess1==x):
    print "Great! Your first guess:",guess1,"got my number exact!"
elif (guess2==x):
    print "Good job! Your second guess:",guess2,"got my number exact!"
elif (distance1>distance2 and distance1<x):
    print "The number was:",x
    print "Your first guess:",guess1,"was closer to my number."
elif (distance1<distance2 and distance1>x):
    print "The number was:",x
    print "Your first guess:",guess1,"was closer to my number."
elif (distance2>distance1 and distance2<x):
    print "The number was:",x
    print "Your second guess:",guess2,"was closer to my number."
elif (distance2<distance1 and distance2>x):
    print "The number was:",x
    print "Your second guess:",guess2,"was closer to my number."

this one i'm having trouble with, for this one i had added a bit more to it to look a bit nicer but after that it will run the program but it won't do the if statements, im not sure if when i was sick that i missed something that we were taught that day or something, im pretty sure i have it right, this one i also had to do in java and i will post another thread over there when im done with this one, and any help is appreciated :),

and the second one (new one):

this program is to get the computer to try and find the largest number that is given but only after a negative number is entered but im not sure what im doing wrong and if i also missed something that was taught, or if there is something in there that is not being used or if i have to add something that will make something else be used.

largest = 0
number = 0
number = int(raw_input("Please enter a number: "))
while (number>0):
    number = int(raw_input("Please enter anohter number: "))
if (largest>number):
    number = largest
    if (number<0):
        print "The largest number was: ",number

again im not sure what im doing worng but any hints, help and suggestions is appreciated. im really trying to advance in my computer science classes as i would like to find a job some day after college in IT work since there is a big demand for it everywhere.

thanks in advance,

strong guy :)

Recommended Answers

All 3 Replies

You should take absolute value in distance calculation and you should update the maximum inside while in second code.

You should take absolute value in distance calculation and you should update the maximum inside while in second code.

ok, i haven't learned how to use absolute values at all so could you just explain what it does and just give a couple of examples of what it does. and im not sure i understand what you mean by updating the maximum inside the while loop, could also explain what you mean by this, sorry i haven't learned a lot yet, just a handful of things during class.

Everything should be under the while loop.

number = int(raw_input("Please enter a number: "))
largest = number
while (number>0):
    number = int(raw_input("Please enter anohter number: "))

    if (largest>number):     ## this will give the smallest
         number = largest
    ##if (number<0):    not necessary as the while loop takes care of this
print "The largest number was: ",number
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.