Why isn't this code working ? (Python) [duplicate] - python

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 7 years ago.
So I'm making a code that accepts a number with only 8 or 7 digits and then if the user enters an 8 digit number then it should add all 8 digits together then divide by 10 and print out the answer. I have been trying to change the user's input into a list but it hasn't been working out.
My current code (not working):
NumGiven=''
while not NumGiven.isnumeric():
NumGiven=(input('Please enter a 7 or 8 digit number:'))
while len(NumGiven)<7 or len(NumGiven)>8:
NumGiven=(input('Please enter a 7 or 8 digit number:'))
if len(NumGiven)==8:
list=[int(i) for i in NumGiven.split()]
I think there is something wrong with the last line, I looked at many other topics but they never seemed to work. Can some one help me tweak this code.

NumGiven.split() splits on whitespace, but there probably isn't any. Since you want to iterate over characters, you can just eliminate the .split().
list=[int(i) for i in NumGiven]

OP asked for the sum - it should be:
print(sum([int(i) for i in NumGiven])/10.0)

Related

How to loop through a string of letters and numbers and identify which are numbers? [duplicate]

This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
Closed 1 year ago.
I am writing some code, and would like to add a conditional statement such that as we loop through stringg below, if we hit an integer, then ... we do something (doesn't matter what)
stringg = "3[a]2[bc]"
But the thing is that even the 3 and 2 are strings at the moment- how can I loop through stringg while checking if each character is an integer?
for i in stringg:
if isinstance(i,int):
print("Hello")
The above loop returns nothing to my point made above. Any ideas how the above can be achieved?
One other thing that came to mind is to do int(i) as we loop, but I couldn't figure out how to do this.
stringg = "3[a]2[bc]"
for ch in stringg:
if ch.isnumeric():
print(ch)

input() function not working in Sublime text [duplicate]

This question already has answers here:
Can't send input to running program in Sublime Text
(5 answers)
Closed 2 years ago.
I have the following function:
years=2
double_rate=(years*12)//3
number_of_rabbits=11
answer=number_of_rabbits*double_rate
print(answer)
I want to generalize the code by being able to input years and number_of_rabbits variables
years=input("select number of years: ")
print(years)
double_rate=(years*12)//3
number_of_rabbits=input("select number of rabbits: ")
print(number_of_rabbits)
answer=number_of_rabbits*double_rate
print(answer)
However the editor (sublime text) only prompts me for the first input variable. I am not able set the second one, "select number of rabbits", nor does it print the new answer
Does anyone know why this is the case?
The number of years you enter is saved as a string, not an int (or float). So when you try to calculate double_rate, you're multiplying a string by 12 (which is fine) and then floor dividing the result by 3, which doesn't work.
Try years = int(input("Select number of years: ")) instead.

FizzBuzz question, I'm unable to solve this code. Read the below output and give the answer please [duplicate]

This question already has answers here:
Python FizzBuzz [closed]
(11 answers)
Closed 2 years ago.
It takes an input n and outputs the numbers from 1 to n.
For each multiple of 3, print "Fizz instead of the number.
For each multiple of 5, prints "Buzz" instead of the number.
For numbers which are multiples of both 3 and 5, output "FizzBuzz".
You need to make the code to skip the even numbers, so that the logic only applies to odd numbers in the range
Adding on to what #dratenik referred, for that last condition to skip for even numbers,
you just need to add following :
def fizz_buzz_main(N):
for i in range(1, N+1):
if N & 1: # check for odd number, then only call, otherwise skip
fizz_buzz() # function already shown by #dratenik
I hope it was clear !
Sample run :
input : n = 10
output : ["1","Fizz","Buzz","7","Fizz"]

Regex that matches only numbers that are divisible by 7 short version [duplicate]

This question already has answers here:
Check number divisibility with regular expressions
(7 answers)
Closed 2 years ago.
I have this question where I need to determine if a number is divisible by 7 using only REGEX in python.
This is what I came up with:
0 7 14 21 ... 91 98 The numbers that appear are: 0-9 for the first and second
and all the trailing left digits can appear as many as they want so \d*
the regex is: \d*\d\d - did the opposite, it returned true for numbers that were not divisible by 7
for example re.match(theReg, '32780') returned False and I need it to return True, so I negated the whole regex to the very final:
~\d*\d\d
This SOMEHOW works for all the numbers, but again, the opposite, it returns False for numbers that are divisible by 7...
Another question: I did not seem to find any way to negate a regex, so how the hell does ~ do all the work?
Thanks!
Your program checks if the answer from re.match(reg, strn) the same as what you need\want it to be - a number divisible by 7 (so you are checking if the output is equal to the number % 7).
This is where you get things wrong, your final Regex returns None for any string containing numbers - and so of course it will "get right" all the strings that contain numbers that are not divisible by 7, in fact it will get anything you wish the answer to be is Falsy!
Making a REGEX that could determine if a number is a multiple of 7 definitely exists, but it is probably very complicated and nasty. Check out the discussion here (Check number divisibility with regular expressions) for more info.
As for the regex ~\d*\d\d it would match any string that starts with ~ and have at least 2 digits. I'm not exactly sure the logic of this string.

how can i count the number of times a certain string occurs in a variable? [duplicate]

This question already has answers here:
String count with overlapping occurrences [closed]
(25 answers)
Closed 4 years ago.
im just learning how to program on edx, and theres this question that i just can seem to understand not to even talk of solving. please i need solutions and idea on how to understand the question and implement it
QUESTION-
Assume s is a string of lower case characters.
Write a program that prints the number of times the string 'bob' occurs in s. For example, if s = 'azcbobobegghakl', then your program should print
Number of times bob occurs is: 2
Since you are just learning, start with basic method.
Simplest apporch will be start from the begining consider all the substrings of the length of the substring that is needed to be counted. If there is a match increase the count.
You can refer the following code to get an idea.
s = 'azcbobobegghakl'
sb = 'bob'
results = 0
sub_len = len(sb)
for i in range(len(s)):
if s[i:i+sub_len] == sb:
results += 1
print(results)

Categories