-2

Whenever i input '10' from the user input or any other number, it prints out'1,0' instead of '10'. Upon my curiosity i am wandering if there's a way to prevent this?

pin1 = []
pin2 = []
frames = []

open = 0
while open != 10:
    open += 1
    if open == 10:
        print("You're on the last frame")
    pin1 = [int(i) for i in
            input(f"You are on frame {message}/10 \nHow many pins did you knock over on your first roll?:")]
    message += 1

    pin2 = [int(i) for i in input("How many pins did you knock over on your second roll?:")]

    frames.append({
        "First roll": pin1,
        "Second roll": pin2
    })
    print(frames)
    total = (pin1 + pin2)
    sum = 0
    for num in total:
        sum += num
    print(f"Your rolls for this frame are, {sum}")

every other number but multiple digit numbers work and they get added but if its more than one its separated.

GLaDOS
  • 1
  • 3
  • 4
    Why did you use a list comprehension? Simply change `[int(i) for i in input(...)]` to `int(input(...))`. – mkrieger1 Dec 16 '19 at 17:17
  • 1
    Does this answer your question? [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Sayse Dec 16 '19 at 17:18
  • i tried that but in order for me to print my list of it telling me the rolls and the overall sum i had to use the append – GLaDOS Dec 16 '19 at 17:19
  • minor remark: `open` and `sum` are a built-in functions, don't redefine them. – FObersteiner Dec 16 '19 at 17:22

2 Answers2

1

With this line you are interpreting every character of the input as a separate number.

pin1 = [int(i) for i in input(f"LONG IRRELEVANT PROMPT STRING")]

If you expect multiple numbers from the user, use split to separate words, not characters.

pin1 = [int(i) for i in input(f"LONG IRRELEVANT PROMPT STRING").split()]

But it seems more like you expect just a single number, so just parse the input directly to int:

pin1 = int(input(f"LONG IRRELEVANT PROMPT STRING"))

Same for pin2, of course. Or if you need those as one-elemented lists:

pin1 = [int(input(f"LONG IRRELEVANT PROMPT STRING"))]
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • will that append it an list that i can still print out and add the sum of them? – GLaDOS Dec 16 '19 at 17:21
  • @GLaDOS I've shown you three variants, some of which use a list (which you do not seem to need at all) and others don't. One of those will work for you. – tobias_k Dec 16 '19 at 17:22
  • okay thank you, the last solution worked, its just i didnt do it the pin1 only pin2 – GLaDOS Dec 16 '19 at 17:29
1

input will return a string which when you then iterate over will return you each character of the string one by one. so if you done for i in '10' i would first be set as 1, then set as 0. Instead there is no need to iterate over the string, just convert it to an int. I have updated your code and also cleaned it up a little bit.

the key here is instead of iterating over input converting each char to an int, it instead takes all of input as a string and converts it to an int.

frames = []
while len(frames) != 10:
    if len(frames) == 9:
        print("You're on the last frame")
    pin1 = int(input(f"You are on frame {len(frames)+1}/10 \nHow many pins did you knock over on your first roll?:"))
    pin2 = int(input("How many pins did you knock over on your second roll?:"))
    frames.append({
        "First roll": pin1,
        "Second roll": pin2
    })
    print(frames)
    print(f"Your rolls for this frame are, {pin1 + pin2}")

TAIL OF OUTPUT

You are on frame 9/10 
How many pins did you knock over on your first roll?:4
How many pins did you knock over on your second roll?:4
[{'First roll': 5, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}]
Your rolls for this frame are, 8
You're on the last frame
You are on frame 10/10 
How many pins did you knock over on your first roll?:5
How many pins did you knock over on your second roll?:5
[{'First roll': 5, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 4, 'Second roll': 4}, {'First roll': 5, 'Second roll': 5}]
Your rolls for this frame are, 10
Chris Doyle
  • 10,703
  • 2
  • 23
  • 42