Python: How do I remove the + at the end? [duplicate] - python

This question already has answers here:
How to avoid last comma in python loop [duplicate]
(3 answers)
Closed 1 year ago.
When I type a number, for example, 3, I want the output to be 1+2+3, not 1+2+3+, how do I fix that?
number = int(input("Please enter a number: "))
for i in range(1, number + 1):
print(i, end="+")

One easy way is to use join instead of repeated print statements.
print('+'.join(str(i) for i in range(1, number + 1)))
Or using map instead of for:
print('+'.join(map(str, range(1, number + 1))))

Try something like this:
number = int(input("Please enter a number: "))
for i in range(1, number + 1):
if i == number:
print(i)
else:
print(i, end="+")

Related

How to print sum of digits for a Python integer? [duplicate]

This question already has answers here:
Sum the digits of a number
(11 answers)
Closed last year.
Consider the following input prompt. I want to output the sum of digits that are inputted.
Example:
two_digit_number = input("Type a two digit number: ") 39
# program should print 12 (3 + 9) on the next line
If I input 39, I want to output 12 (3 + 9). How do I do this?
You can use sum(), transforming each digit to an integer:
num = input("Enter a two digit number: ")
print(sum(int(digit) for digit in num))
maybe like this:
numbers = list(input("Enter number: "))
print(sum(list(map(int, numbers))))
Read digits with input as a whole number of string type
split them with list() in characters in a list
use map() to convert type to int in a list
with sum() you're done.. just print()
Beware of invalid entries! "hundred" or "1 234 567"
you can achieve that like this:
a = input("Type a two digit number: ")
b = list(a)
print(int(b[0])+int(b[1]))
n = (int(input("Enter the two digit number\n")))
n = str(n)
p = int(n[0])+int(n[1])
print(p)
This is one of the many ways for it.
value = 39 # it is your input
value_string = str(value)
total = 0
for digit in value_string:
total += int(digit)
print(total)
new version according to need mentioned in comment:
value = 39
value_string = str(value)
total = 0
digit_list = []
for digit in value_string:
total += int(digit)
digit_list.append(digit)
print(" + ".join(digit_list))
print(total)
You can do that as follows:
two_digit_number = input("Type a two digit number: ")
digits = two_digit_number.split("+")
print("sum = ", int(digits[0])+int(digits[1]))
Explanation:
first read the input (ex: 1+1)
then use split() to separate the input statement into the strings of the input digits only
then use int() to cast the digit from string to int

Python composed Lists [duplicate]

This question already has answers here:
Check if a number is odd or even in Python [duplicate]
(6 answers)
Closed 1 year ago.
I would just like a tip on how to send even numbers to the left and odd numbers to the right, I've been trying for a long time and I can't, the subject is composed lists, I appreciate any help. Simplified code below without loop or conditional structure.
test = [[],[]]
num = int (input("Type it : "))
test.append()
print(test)
test = [[], []]
num = input("Enter a number or quit")
while num != "quit": # execute code as long as num is not quit
num = int(num) # convert num to a number (until here, it's a string !!!)
if num % 2 == 0: # num is even
test[0].append(num)
else: # num is odd
test[1].append(num)
print(test)

Output smiley faces based on number input [duplicate]

This question already has answers here:
How to print something a specific number of times based on user input?
(3 answers)
Closed 1 year ago.
The code is supposed to print :) based on the number inputted (an integer 1 to 10). With any positive integer, the code is supposed to print that many smiley faces (ex. if 5 is entered, 5 smiley faces should be printed).
It's required that the code should use += to add onto the end of a string and should also decrement to count down and use a loop.
x = input("enter number 1 to 10: ")
for i in len(x):
print(":) " * x)
I don't think you can multiply int values and str values, and I can't find another way to do this.
First things first, input() function returns a string. You want x to be an integer, use the int() function:
x = int(input("enter number 1 to 10: "))
Second You can either use a for loop or the multiplication operator to print n number of smileys.
# Using for loop to print n smileys :
x = int(input("enter number 1 to 10: "))
for i in range(x):
print(":) ",end="")
# Using multiplication operator :
x = int(input("enter number 1 to 10: "))
print(":) "*x)
# x should be an integer
x = int(input("enter number 1 to 10: "))
# define "s" as an empty string
s = ''
# increment ":)" to "s" x times
for _ in range(x):
s += ':)'
print(s)
You're almost there! In fact, you can multiply string and integer values. This code does what you described:
print(':) '*int(input("Enter number 1 to 10: ")))
If you really want to use a for loop and +=, you could also do this
x, s = input("enter number 1 to 10: "), ''
for i in range(x):
s += ':) '
print(s)
Answer: How to print a string x times based on user input
Credits to: NoobCoder33333
times = input('Enter number from 1 to 10: ')
word = ":-)"
print('\n'.join([word] * int(times)))

Python Calculator with lists and loops [duplicate]

This question already has answers here:
for loop in python with decimal number as step [duplicate]
(2 answers)
Closed 1 year ago.
I am trying to create a calculator program that takes user input and stores it in a list and perform the calculation based on their selection. However, I keep running across an error for line 31 saying
TypeError: 'float' object cannot be interpreted as an integer.
Here is the code:
import math
def printIntro():
print("This is a simple calculator.")
print("Select an operation:\n1) Add\n2) Subtract\n3) Divide\n4) Multiply")
while True:
operator = input("Enter your choice of + - / *: \n")
if operator in('+', '-', '/', '*'):
#continue asking for numbers until user ends
#store numbers in an array
list = []
num = float(input("What are your numbers?: "))
for n in range(num):
numbers = float(input("What are your numbers?: "))
list.append(numbers)
if n == '':
break
if operator == '+':
print(add(list))
if operator == '-':
print(subtract(list))
if operator == '/':
print(divide(list))
if operator == '*':
print(multiply(list))
else:
break
Python's range method accepts an integer.
The error is due to the fact that you convert the input to float in num = float(..) before giving it to range(num).
I think in the line where you try to get the number from input causes it:
num = float(input("What are your numbers?: "))
It should work with (I also fixed the prompt message):
num = int(input("How many numbers you have?: "))
for n in range(int(num)): cast num as int inside the for loop such as this. There is a similar comment to this above but here is the fixed code.

How to find a specific number from a user inputed list in python [duplicate]

This question already has answers here:
Python - Count elements in list [duplicate]
(7 answers)
Closed 7 years ago.
I want the program to find the number of times a particular number occurs within a list. What am I doing wrong here?
def list1():
numInput = input("Enter numbers separated by commas: ")
numList = numInput.split(",")
numFind = int(input("Enter a number to look for: "))
count = 0
for num in numList:
if num == numFind:
count += 1
length = len(numList)
# dividing how many times the input number was entered
# by the length of the list to find the %
fraction = count / length
print("Apeared",count,"times")
print("Constitutes",fraction,"% of this data set")
list1()
numList isn't a list of numbers, it's a list of strings. Try converting to integer before comparing to numFind.
if int(num) == numFind:
Alternatively, leave numFind as a string:
numFind = input("Enter a number to look for: ")
... Although this may introduce some complications, e.g. if the user enters 1, 2, 3, 4 as their list (note the spaces) and 2 as their number, It will say "Appeared 0 time" because " 2" and "2" won't compare equal.
There are 2 issues with the code, First you are comparing int with str and the second is count / length. In Python when you divide int with int you get an int returned not a float (as expected.) so fraction = flost(count) / length would work for you , also you need to convert all the elements in the list to integers which can be done as :
numList = map(int, numInput.split(","))

Categories

Resources