Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find nearest number
#1
so first here is my code.

    elif medicin in ("adrenalin", "ad"):
        vaegt = int(input("Hvad vejer patienten? "))  #what does the patient weight
        dosis_adrenalin = vaegt * 0.01                # Dose to be given
        if vaegt >= 50:                               # if vaegt > 50 give 0,5 mg
            print("indgift = 0,5 mg")
            print("volumen = 0,5 ml")

        else:
            if vaegt in (10, 11, 12):    #This does nothing
                vaegt == 10              # this does nothing
            print(f"dosis = {dosis_adrenalin} mg") #it runs this part
            print(f"volumen = {dosis_adrenalin} ml")
    else:
        print("Kan ikke finde medicinen!")
i want the code to find the nearest "5"
if vaegt == 12, then I want it to be 10
if vaegt == 13, then I want it to be 15

I want this to go from 10-50. (10, 15, 20, 25 etc.)
Reply
#2
vaegt % 5 is how far vaegt is above the next lowest five. So if vaegt % 5 is 0, 1, or 2, you want to subtract vaegt % 5 from vaegt. If vaegt % 5 is 3 or 4, you want to do the same thing, but then add five more.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Aug-26-2019, 02:20 PM)ichabod801 Wrote: vaegt % 5 is how far vaegt is above the next lowest five. So if vaegt % 5 is 0, 1, or 2, you want to subtract vaegt % 5 from vaegt. If vaegt % 5 is 3 or 4, you want to do the same thing, but then add five more.

could you give me a code exampel? I dont qiute understand.
Reply
#4
Why don't you try to code it? Then I can help you if it doesn't work.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Aug-26-2019, 02:34 PM)ichabod801 Wrote: Why don't you try to code it? Then I can help you if it doesn't work.
I dont understand the "%"

i could do it like this

elif vaegt in (10, 11, 12):
vaegt = 10
elif vaegt in (13, 14, 15, 16, 17):
vaegt = 15
  #repeat        
but the output I get is messed up.
it would say: dosis = 0,10000000003 mg

what I want is a list ranging from 10-50

list [10, 15, 20, 25, 30, 35, 40, 45, 50] #this is the list
vaegt = int(input("Hvad vejer patienten? ")) #what does the patient weight
to_be_used_veagt = nearest from list #the weight I have to use is the nearest from the list 
Reply
#6
% is the modulo division operator. That is x % y gives the remainder after dividing x by y:

>>> 4 % 3
1
>>> 4 % 8
4
>>> 4 % 4
0
>>>
Reply
#7
Thanks ichabod801 & ndc85430
That solved my problem.
This must be very simple coding for you. But for me this is an achivement. Cant get my arms down

this is how the outcome is from your help.
vaegt = int(input("Hvad vejer patienten? "))     #what is the weight?
test = vaegt % 5     #the weight must be the nearest "5", so how many times does 5 goes up into the weitht. get the remaining.
print(test)          #print the remaining, this will be deleted from the final code.
if test in (1, 2):   # if the remaining is 1 or 2
    vaegt = vaegt - test    #take the weight and subtract 1 or 2  
    print(vaegt)
elif test in (3, 4):    #if the remaining is 3 or 4
    vaegt = vaegt - test + 5 #take the weight and subtract 3 or 4 and then add 5
    print(vaegt)
Reply
#8
(Aug-26-2019, 08:24 PM)d3fi Wrote: But for me this is an achivement. Cant get my arms down

Would your arms be up if we just gave you the code?

Now, here's how I might have done it:

vaegt = int(input('Hvad vejer patienten? '))
remainder = vaegt % 5
vaegt -= remainder
if remainder > 2:
    vaegt += 5
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find the sum of a series of values that equal a number ancorte 1 461 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  Get numpy ceil and floor value for nearest two decimals klllmmm 4 1,201 Jun-07-2023, 07:35 AM
Last Post: paul18fr
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,016 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  compare and find the nearest element ? mr_gentle_sausage 4 1,003 Jan-15-2023, 07:11 AM
Last Post: DPaul
  Find if chain of characters or number Frankduc 4 1,752 Feb-11-2022, 01:55 PM
Last Post: Frankduc
Question Help to find the largest int number in a file directory SalzmannNicholas 1 1,587 Jan-13-2022, 05:22 PM
Last Post: ndc85430
  Regular expression: cannot find 1st number in a string Pavel_47 2 2,365 Jan-15-2021, 04:39 PM
Last Post: bowlofred
  Rounding to the nearest eight wallgraffiti 2 2,020 Jul-15-2020, 06:05 PM
Last Post: wallgraffiti
  Find the complement of a number landlord1984 4 32,566 Feb-21-2020, 04:26 AM
Last Post: shreyaspadhye3011
  Find Average of User Input Defined number of Scores DustinKlent 1 4,205 Oct-25-2019, 12:40 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020