Home > other >  Using Random and Range together
Using Random and Range together

Time:09-05

I would like to utilise random and range together in this code. Im trying to figure how the user can input a weight unit (kg or pounds) and the output would be a range of weights between 10 and 200, and only generate say 100 random weights between that range. I think its only a small fix, I just can't nut it out where it should go.

Code so far..

import random

weight_unit = input("Enter the units: , kg or pounds")

while weight_unit != 'pounds' or weight_unit != 'kg':
    
if weight_unit =='pounds':
        for i in range(10,200):
            print("weight_unit" , i 1, '=', i,'pounds')
        break
    
    elif weight_unit == 'kg':
        for i in range(10,200):
            print("weight_unit ", i 1, "=" ,round(i/2.204,2),"kg")
        break
        
    else :
        weight_unit = input("Incorrect input. Reinput the unit kg or pounds")

CodePudding user response:

I am not sure I 100% understand the request. However, if you are trying to obtain a random number between 10 and 200 you can use the following code to put a random number between 10 and 200 inside of variable ran_value:

ran_value = random.randint(10,201)

Where 10 is the lower limit and 201 is the upper limit (not inclusive)

https://www.askpython.com/python-modules/python-random-module-generate-random-numbers-sequences

The above site has documentation on the random module and all the random functions that come with it.

If you are also trying to achieve an output that has a random number of outputs. You can use a while loop that increments up to a random number that you can select with via the same code as above like so:

ran_range = random.randint(50,150)
i = 1
while i <= ran_range:
     Run your code
     i  = 1

CodePudding user response:

Maybe you can try this one

import random

weight_unit = input("Enter the units: , kg or pounds")

while weight_unit != 'pounds' or weight_unit != 'kg':

    if weight_unit =='pounds':
        for i in range(100):
            print("weight_unit" , i 1, '=', random.randint(10, 200),'pounds')
        break

    elif weight_unit == 'kg':
        for i in range(100):
            print("weight_unit ", i 1, "=" ,round(random.randint(10, 200)/2.204,2),"kg")
        break

    else :
        weight_unit = input("Incorrect input. Reinput the unit kg or pounds")

CodePudding user response:

try this:

import numpy as np

weight_unit = input("Enter the units: , kg or pounds")

while weight_unit != 'pounds' or weight_unit != 'kg':

    # rng    = np.random.default_rng(12345)
    rints  = rng.integers(low=10, high=200, size=1).tolist()[0]
    if weight_unit =='pounds':  
            print("weight_unit" , rint 1, '=', rint,'pounds')
            break
    
    elif weight_unit == 'kg':
            print("weight_unit ", rint 1, "=" ,round(rint/2.204,2),"kg")
            break
        
    else :
        weight_unit = input("Incorrect input. Reinput the unit kg or pounds")

CodePudding user response:

try this:

import random
import numpy as np

weight_unit = input("Enter the units: , kg or pounds")

while weight_unit != 'pounds' or weight_unit != 'kg':

    rng    = np.random.default_rng(12345)
    rints  = rng.integers(low=10, high=200, size=1).tolist()[0]
    if weight_unit =='pounds':  
            print("weight_unit" , rint 1, '=', rint,'pounds')
            break
    
    elif weight_unit == 'kg':
            print("weight_unit ", rint 1, "=" ,round(rint/2.204,2),"kg")
            break
        
    else :
        weight_unit = input("Incorrect input. Reinput the unit kg or pounds")

CodePudding user response:

This works to me.

if weight_unit =='pounds':
    for i in range(100):
        print("weight_unit" , i 1, '=', random.randint(10, 200),'pounds')
    break

CodePudding user response:

Dictionary used to manage measures. Comments in text

import random

# the dictionary contains 2 weight measures as keys, and the names of alternative weight measures
# and conversion formulas from one measure to another as values
measures = {'pounds': ['round(weight / 2.204, 2)', 'kg'],
            'kg': ['round(weight * 2.204, 2)', 'pounds']}

prompt = f"Enter the units: {list(measures.keys())}:"  # initial prompt for text entry (before the first incorrect entry)

while True:  # an infinite loop, the exit from which (break) will occur when you enter a measure corresponding to any of the keys of the dictionary
    weight_unit = input(prompt)
    if weight_unit in measures.keys():  # dict_keys(['pounds', 'kg'])
        break
    prompt = f"Incorrect input. Reinput the unit {list(measures.keys())}: "  # subsequent text input prompts (after incorrect input)

for i in range(100):
    weight = random.randint(10, 201)
    # the weight is displayed in the main (user-selected measure) and in the alternative (other)
    # with the conversion of values ​​by the formula through eval()
    print(f'weight_unit # {i   1} = {weight} {weight_unit} ({eval(measures[weight_unit][0])} {measures[weight_unit][1]})')

Output:

Enter the units: ['pounds', 'kg']:aaa
Incorrect input. Reinput the unit ['pounds', 'kg']: kg
weight_unit # 1 = 74 kg (163.1 pounds)
weight_unit # 2 = 145 kg (319.58 pounds)
weight_unit # 3 = 163 kg (359.25 pounds)
weight_unit # 4 = 196 kg (431.98 pounds)
weight_unit # 5 = 161 kg (354.84 pounds)
weight_unit # 6 = 67 kg (147.67 pounds)
...

  • Related