input which number in row and output the number [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 10 months ago.
n=[0]*365
s=0
s1=1
s2=2
s3=3
s4=4
s5=5
s6=6
for i in range(len(n)):
n[i]=4+1
s=s+7
s1=s1+7
s2=s2+7
s3=s3+7
s4=s4+7
s5=s5+7
s6=s6+7
if n[i]==s:
n[i]=0
elif n[i]==s1:
n[i]=1
elif n[i]==s2:
n[i]=2
elif n[i]==s3:
n[i]=3
elif n[i]==s4:
n[i]=4
elif n[i]==s5:
n[i]=5
elif n[i]==s6:
n[i]=6
Hey everyone here is my code i need to imput which number in row in array it is and it needs print out the number like if i input 300 it needs to output the 300th number in the list
I have tried using print(n[input()] but that obvioslly didnt work can you please help me

It is because input returns a string. The following should work:
print(n[int(input())])
However, note that this will fail if you input something else than a number as python will fail in the int conversion.

Related

I write a python program to ask my computer to guess a secret number but it does not work [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed last year.
Here is my code:
import random
def guessGame(x):
low=1
high=x
feedback=" "
while feedback != "c":
if high!=low:
guess=int(random.randint(low,high))
else:
guess=low
feedback= input(f"Is it {guess} too high(H),too lowe(L) or correct(C)?").lower()
if feedback == "h":
high = guess-1
elif feedback == "l":
low=guess+1
print(f"I guess the correct number{guess}!")
x=input("Please input a highest number in the range: ")
guessGame(x)
I wrote a python code to ask my computer to guess a secret number. But it did not work. I did some changes but still do know where I did wrong.
Python input type is a string you must convert to string to integer
x=int(input("Please input a highest number in the range: "))

Split string into two integers, python [duplicate]

This question already has answers here:
Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced
(4 answers)
Closed 1 year ago.
I have problem and trying to get next:
new_string = "35" #and this result must be like new_int = 3+5.
How im available to do this? I know the type conversion, but not a clue how i should do this.
As you are new to the python, i suggest you doing it using
int(new_string[0]) # 3
int(new_string[1]) # 5
So now you have 2 integers, you can to whatever you want

Errors when converting input() function into an integer [duplicate]

This question already has an answer here:
Python - How to break while loop after empty value in a int turning input? [duplicate]
(1 answer)
Closed 2 years ago.
I have been trying to simply turn an input() function into an integer as the title of this question suggests. I am essentially trying to run a program that takes in as many inputs as the user inputs, but when an empty string is inputted, it breaks out of a loop and returns the average of all inputted numbers. Currently, my code looks like this:
count = 0
sum = 0.0
number = 1.0
while number != 0:
number = int(input(""))
sum = sum + number
count += 1
if number == 0:
continue
if number == "":
break
else:
print("Average is {}".format(sum / (count-1)))
The issue i face is the error:
ValueError: invalid literal for int() with base 10: ''
Does anyone have a simple solution for this? I feel like i'm overlooking something rather simple?
if number == "":
break
In the case where you want this to happen, number got its value as int(input()). That is, the attempt to convert to int happens first. Since an empty string cannot be converted to int, this test is not reached before the exception is thrown.
You should test for the exception anyway, using try:/except:. But if you want to compare a string to a string, you need to do it at the point where you still have the strings you want to do the comparison with.

How to make a certain number be counted rather than the length of the actual number? [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 5 years ago.
What I want to happen is if the number is over 500 for the console to print something, but right now it counts how long the number is not how high its numerical value is.
print ("What Is The Current Distance In Meters?")
distance = input ()
if len (distance) >= 500:
(print) ("Thanks")
You need to first convert distance to a number and then use the greater than operator > so you code becomes:
print ("What Is The Current Distance In Meters?")
distance = input ()
if int(distance) > 500 :
print ("Thanks")
Hope I helped!
input() is always a string - you likely want distance = int(input())

Center str in python 2.7 [duplicate]

This question already has answers here:
Center-aligning text on console in Python
(3 answers)
Closed 9 years ago.
i'm trying to center the text of a string but i can't do it. I checked mutiple posts and tried what they said, i don't get an error but the text is still aligned to the left. here the code and the str i want to center
while True:
print ''
while True:
try:
userNum = float(raw_input('Enter the total of your bill: '))
if (userNum) > 0 and (userNum) != 0:
break
else:
print 'Oops! That was no valid number. Try again...'
You could use str.format. The ^80 format tells Python to make the string 80 characters long, and to center the argument:
print('{:^80}'.format('Oops! That was not a valid number. Try again...'))
yields
Oops! That was not a valid number. Try again...
take a look at this str.center

Categories

Resources