Read user input numbers and perform a calculation in Python - python

please help me... "Read in two numbers from user input without a prompt, add them, and print the result.
Hint: Use int() to convert the numbers to integers.
Note: These activities may test code with different test values. This activity will perform two tests: the first with num1 = 5 and num2 = 10, the second with num1 = 6 and num2 = 3. "
I tried just doing one and it didn't work but I can't wrap my head around how to do two tests let alone one...
I tried this so far and it came out with 510..noob please help
num1=int(input(5))
num2=int(input(10))
num3=num1 + num2

The argument to the input() function is the string to display as a prompt.
In your case, you'd want that to be simply input().

in fact you need just:
num1 = int(input())
num2 = int(input())
num3 = num1+num2
print(num3)

Don't pass parameters into input. You're asking the user for input.
I don't think the assignment is asking you to write your own tests. I think it's saying that whatever code you give it will be tested.

person_name = input()
person_age = int(input())

Related

Multiplication in Jupyter Notebook

I am very new to python and coding as a whole, so this might be something very basic. I am trying to execute this code, but instead of multiplying the value, it just doubles the numerical. I have tried changing it multiple times in different places, but nothing works. Here is the code:
num=input("Enter a number:")
doub=num*2
print("Double of {0}, is {1}".format(num,doub))
If num is 4, instead of outputting num*2 as 8, it outputs it as 44.
I attached the link to the image as well, hopefully, it is displayed properly here, while I earn some reputation points lol.
Thanks!
You need to convert the num variable from string to integer or decimal:
num = input("Enter a number:")
doub = int(num) * 2 # <-- HERE
print("Double of {0}, is {1}".format(num, doub))

Can I ask the user to input any basic arithmetic equation without having to ask for every individual number to be computed?

I am a complete beginner in coding.
I want to make a simple math calculator using Python.
Instead of the traditional method of asking the user to input 2 separate integers
(in this example addition)
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
total = num1 + num2
print(total)
I would like the user to immediately input
1+1
or any other simple math that may involve one or a combination of the following: addition, subtraction, multiplication, division.
Is it possible to do so?
you could also separate the input into a list. For example, when you input "1 + 1" there are functions in Python to separate all the characters in the list. It can make it ["1", "+", "1"]. With this, you can read each individual character and use them like input[2] to find the operation.

how to print a message multiple times on multiple different lines?

for starters im new to python and not really good at this kind of thing so dont crusify me ive had bad experiences on reddit.
But, Im able to get user input to repeat the string x amount of times but i cant get it to repeat on different lines.
ive tried \n multiple times (the only thing ive used) in multiple different ways but i just get invalid syntax but i havent really tried to change the base code much.
age = int(input("how old are you?: "))
agedifference = 100 - age
print ("you will be 100 in",agedifference,"years") * int(input("pick a number: "))
so basically i need the user to pick a number for example 4 then repeat the phrase 4 times on 4 different lines.
if you can give a solution with an in depth explanation i would greatly appreciate it (no such thing as to in depth im noob)
Try this (assuming you are on python3):
age = int(input("how old are you?: "))
agedifference = 100 - age
print(f"you will be 100 in {agedifference} years\n" * int(input("pick a number: ")))
For python2:
age = int(input("how old are you?: "))
agedifference = 100 - age
print("you will be 100 in {} years\n".format(agedifference) * int(input("pick a number: ")))
There are plenty of solutions for what you want:
The classic for loop:
for _ in range(num):
print(f"you will be 100 in {agedifference} years")
In python, a print ends the line by default, you can change this behaviour by modifying the end parameter of the function.
The more pythonic one liner:
print('\n'.join([f"you will be 100 in {agedifference} years"] * num))
This one takes advantage of multiple python features:
str.join(iterable) will create a string by concatenating all elements contained in iterable and join them by adding the str in between.
list * int repeats all elements in the list int times.
f-string are one of the many ways to insert variables in string without having to concatenate strings.
You can do something like this as well:
You ask the user to input age and you calculate the agedifference.
Then you ask the user to input the times it has to repeat.
Then use a for loop for looping the output the number of times given by the user.
age = int(input("how old are you?: "))
agedifference = 100 - age
times = int(input("pick a number: "))
for i in range(0,times):
print ("you will be 100 in",agedifference,"years")
What happens with your code is that you are multiplying the return value of print(), which is None, by the number input by the user. Instead, you should repeat the string which is passed in the print() function.
When calling the print() function, here, make sure the string you print ends with a \n. Also, my example needs Python >= 3.6 because of the way the string is formatted (with the 'f' before the string), it is called literal string interpolation.
Finally, to get rid of the last line return which prints an empty line, add the argument end='' to the call to print() (because this character is already in the string your print).
age = int(input("how old are you?: "))
age_difference = 100 - age
repetitions = int(input("pick a number: "))
print(f"you will be 100 in {age_difference} years\n" * repetitions, end='')

Math with direct input

I just started my first day at uni and there is one of the exercises I am already struggling with. This is the problem: Make code that inputs two given mumbers and output one minus the other
I did this:
number1 = int(raw_input("Type your first number: "))
number2 = int(raw_input("Type your second number: "))
result = number1 - number2
print result
But it was wrong because the input is direct so when the program tested my code it said:
I have always done code that first asks for the information so I have no idea if this is even possible in python or how you do it. Any suggestions are appreciated, thanks.
you need to use :
number1, number2 = map(int, raw_input().split())
result = number1 - number2
print result
In the sample input both the numbers are separated by space, and there is no prompt for the user such as "Type your first number: ", So all yo need to do is take input using raw_input(), then splitting the input on " "(space) by using .split() and then converting each string after splitting to int using the map function.

Generating random numbers in python

I'm trying to make a basic dice roller. When i run this program in Codeskulptor, it throws an error on the randint function. Can I not set the range for it using raw_input plugged into variables? Is there a different function I should use?
"""Program to roll random numbers within the ranges set."""
import random
sides_of_die=raw_input("Enter how many sides your die has: ")
number_of_dice=raw_input("Enter number of dice you have: ")
total=sides_of_die*number_of_dice
rollinput=raw_input("Would you like to roll now?")
rollinputcap=rollinput.upper()
if rollinputcap =="Y":
print random.randint(number_of_dice,total)
else:
print "What do you want then?"
raw_input() returns a string, not an integer. To convert it to an integer type, use int():
sides_of_die = int(raw_input("Enter how many sides your die has: "))
number_of_dice = int(raw_input("Enter number of dice you have: "))
What's happening in your code is, you may input "6" and "2", so when you do total = sides_of_die * number_of_dice, you're getting a TypeError
This is just because raw_input returns a string, not a number, while randint accept two numbers as arguments
so you should do
total = int(raw_input(..))
Point is, this is not always secure. Exceptions are very likely to be thrown, so you might want to use a try block; but for the time being, I think it's okay (I'm assuming you're just learning Python).
Another thing, which is rather important:
Look at the exception! If you'd read it, you would have known exactly what the problem was.
Beside the raw_input() problem pointed out by the others, #Mark Ransom's comment is important: the sum of dice value eventually follows normal distribution. See: http://en.wikipedia.org/wiki/Dice#Probability
Your:
if rollinputcap =="Y":
print random.randint(number_of_dice,total)
should be changed to
if rollinputcap =="Y":
sum_dice=[]
for i in range(number_of_dice):
sum_dice.append(random.randint(1, sides_of_dice))
print sum(sum_dice)

Categories

Resources