Jump to content

python help!

Bryan 2760

Hi everyone, 

I am doing some python hw and i can seem to figure out what is wrong with my code. I cant seem to get it work. the question. can someone tell me what im missing? thank you

1.     Translate the following pseudocode into a Python program for finding the minimum value and prints it from a set of inputs.  Keep asking user for a number until they enter a sentinel value, storing the number if it’s lower than the number already stored.  When a sentinel value is entered, the program stops. 5 pts

Input value

Set a Boolean variable “first” to true

While input value not equal to sentinel value

      If first is true

                  Set the minimum to the value.

                  Set first to false.

      Else if the value is less than the minimum

                  Set the minimum to the value

      Input value

Print the minimum

 

a)    Your code with comments

b)    A screenshot of the execution

Test Cases:

                                    15, 12, 4, 2, 22, -1 (should print “the smallest number is 2”)

  

Capture.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Bryan 2760 said:

can someone tell me what im missing?

You have the line first == False -- should be first = False

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, WereCatf said:

You have the line first == False -- should be first = False

its not working :l thanks for the suggestion though1.PNG.473a9aaf4c39f32811f1935313238277.PNG

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Bryan 2760 said:

its not working :l thanks for the suggestion though

Another issue is that... well, what if num1 is equal or bigger than minimum? You have nothing to handle that situation. That's the problem: you assign minimum = num1 but then have a comparison of num1 < minimum -- it never passes that comparison since, well, you just made them equal.

 

I assume your intention was that the line num1 = input("input a number:") was supposed to be outside the if-statement, not inside it.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

This is why I, quite often, don't like beginner programming courses in school. The professors/teachers often hide better ways of doing things from students.

 

I know this is homework, I am only showing these because they do not comply to the assignment, which was to implement the given psuedo-algorithm, and therefore cannot be turned in for score.

 

For maximum value:

numMax = 0
numInput = 0

while numInput != -1:
  numInput = int(input("Please enter a number: "))
  
  if numInput > numMax:
    numMax = numInput
    
print("The largest number was {a}" .format(a=numMax))
    


For minimum value:

numMin = 0;
numInput = 0;

while numInput != -1:
  numInput = int(input("Please enter a number: "))
  
  if numInput < numMin:
    numMin = numInput
    
print("The smallest number was {a}" .format(a=numMin))

 

 

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, straight_stewie said:

they do not comply to the assignment

This is why I didn't post better code: the OP is a beginner and I didn't want to confuse them even further with code that isn't of help with their assignment. I don't see confusing the OP as productive.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, WereCatf said:

This is why I didn't post better code: the OP is a beginner and I didn't want to confuse them even further with code that isn't of help with their assignment. I don't see confusing the OP as productive. 

I didn't see it as confusing, I saw it as giving examples of alternate ways to do things.

I would agree if this was something like "Making a simple sorting algorithm" and I posted a generic sorter that takes a lambda to sort objects instead of helping with the sorting algorithm, but this case is no where near that.

In general, I don't believe that it's confusing if both the intended method and the alternative method only require the same level of knowledge to understand. I should probably remove that bitwise bit though.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, straight_stewie said:

I didn't see it as confusing, I saw it as giving examples of alternate ways to do things.
--SNIP--
In general, I don't believe that it's confusing if both the intended method and the alternative method only require the same level of knowledge to understand. I should probably remove that bitwise bit though.

I didn't say it was confusing, I meant that it could be and I didn't want to even accidentally confuse the matters. And yes, you'd think so, but I've seen PLENTY of cases to the contrary over the years.

 

Anyways, don't take my comment as an attack or criticism or anything like that. It was merely an observation and I didn't try to imply anything else with it. I would've written similar code myself, and I agree about teachers generally teaching sub-optimal coding-skills. As for OP: don't be afraid to come back for more help in the future. We may bite, but we only do it out of love.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

you both are very helpful, but is there a way i can fix my code? it would help me understand a lot better if I knew what i did wrong or what i am missing!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Bryan 2760 said:

you both are very helpful, but is there a way i can fix my code? it would help me understand a lot better if I knew what i did wrong or what i am missing!

you input is coming in a a string you need to use

			int(input("Input a number: "))				

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

On 2/20/2019 at 8:31 PM, vorticalbox said:

you input is coming in a a string you need to use

 


			int(input("Input a number: "))				

 

So it looks like it's basic ignoring all comparison operators? Shouldn't it provide a TypeError? 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, ZacoAttaco said:

So it looks like it's basic ignoring all comparison operators? Shouldn't it provide a TypeError? 

int() will automatically parse and cast the string. If the string isn't a valid integer literal, it will give a ValueError.

int("1") #will result in 1
int("one") #ValueError
int("1.5") #ValueError
float("1.5") #will result in 1.5

 

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, reniat said:

int() will automatically parse and cast the string. If the string isn't a valid integer literal, it will give a ValueError.


int("1") #will result in 1
int("one") #ValueError
int("1.5") #ValueError
float("1.5") #will result in 1.5

 

Yeah, I get that, OP should definitely wrap their inputs like so:

int(input("Enter Number"))

There might be some other logic based errors but the main issue is wrong variable types.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, ZacoAttaco said:

There might be some other logic based errors but the main issue is wrong variable types.

nvm I misread. I thought you were saying that code in general would result in casting errors. my bad.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, reniat said:

nvm I misread. I thought you were saying that code in general would result in casting errors. my bad.

It's ok, I was confused too. The OP wasn't explicit in what error they were getting exactly. Hopefully we're on the same page now haha.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×