Why get_int() from cs50 in python is not working? [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 20 hours ago.
Improve this question
I follow cs50 by D.Malan and he presented:
CS50 2022 - Lecture 6 - Python
Time: 1:39:12
from cs50 import get_int
# Get scores
scores = []
for i in range(3):
score = get_int("Score: ")
scores += [score]
# Print average
average = sum(scores) / len(scores)
print(f"Average: {average}")
It loops endlessly without asking for an integer and not returning any result.
I work with VSCode jupyter notebook file.
Code available as well here:
https://cdn.cs50.net/2021/fall/lectures/6/src6.pdf
Why is that ? But during the lecture it quickly returns a value.
I expect the same result like on the lecture YT.

Related

How can i fix this Type Error that appears? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm new to python. For a project at school, I have to make a sort of card game.
here is the code that I use:
playersDeckSize = deckSize / 2
i = 0
for i in range(int(playersDeckSize)):
playerDeck[i] = cards[random.randint(0, len(cards - 1))]
computerDeck[i] = cards[random.randint(0, len(cards - 1))]
i = i + 1
Your problem is in this expression:
len(cards - 1)
cards appears to be a list; you can't subtract 1 from a list. I suspect that you mean
len(cards) - 1

Python List to Float conversion [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 5 years ago.
Improve this question
The result of one of my web scrapes produces the following:
price = ['$1049.98']
which is type list, and I am trying to convert this to float.
Take the first and only element in the list, strip off the $ sign and convert it to a float:
parsed_price = float(price[0].lstrip('$'))
import re
price = ['$1049.98']
get_values = [float(re.search(r'(\d+\.\d+)+',cost).group()) for cost in price]
print(get_values)
>>>[1049.98]
Here is my simple solution. Hope that helps.
price = ['$1049.98']
result = [float(i[1:]) for i in price][0]
print(result)
You can change the index(I mean, replace 0 with other numbers) or loop though the list(result).

"while True" intermediate solutions [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm working with big multidimensional arrays. I want to know after x loops the values S,S2,E,E2
I want to find all the solutions of a loop.
I have a code that iterates with a greedy. My output is a list of numers. I want to save the arrays that generate those numbers. My code:
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
print f2-f
else:
break
If i run this code I have a list of f2-f but I need also to save the f,S,S2,f2,E,E2 created in each passage of the while.. Thank you
results = []
while True:
if f2<f:
f,S,S2=f2,E,E2 # f is a value and S and E are arrays
......
results.append((f,S,S2,E,E2))
print f2-f
print "intermediate result:", results[-1]
else:
break

Please help me understand this - Python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
Can someone please explain how this works? Like, how does it count?
for a in range(2,5):
for b in range(1,2):
print (a+b,end=" ")
print("---",end=" ")
the output is: 3 --- 4 --- 5 ---
It just adds 2, 3, 4 with 1 and prints the result.
In Python-3.x, print() is a function and the argument end in it means
string appended after the last value, default a newline
Values returned by range(x, y) are [x, y-1]
If you don't know the usage of a function, you could open ipython and type something like the following:
help(print)
help(range)

Python 3.0 testing code receiving syntax error where there shouldn't be one [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
def distinct(y):
s = str(y)
for digit in s:
if s.count(digit) > 1:
return False
return True
f = open("s1.15.in", 'r')
year = int(f.readline()) + 1
while not distinct(year):
year = year + 1
print (year)
I'm receiving the syntax error on the "f" variable and I'm not sure why. I'm super new to Python.
IDLE's shell is not meant for pasting code. Create a new script file (File → New Window) and run that script (Run → Run Module); it'll work fine.

Categories

Resources