Python Forum
why doesnt the while loop run?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why doesnt the while loop run?
#1
hi there super beginner here and trying out the rock paper scissors game question using python
just the first portion is posted since the rest should be similar
however it seems that the while loop first line is not even activated, why is this so?

first = input ("rock paper scissors?: ")
second = input ("rock paper scissors?: ")
rock = 5
paper = 10
scissors = 15
print(rock)

while first == 5:
    print ("activated")
    if second == 15:
        print ("congrats player 1")
        break
    elif second == 10:
        print ("congrats player 2")
        break
    else:
        print ("draw")
        continue
thank u very much in advance!!!
Reply
#2
Welcome to the forum!


If you print the 'first' variable you will see that it is a string. However, the while condition is against an integer. So...
The input is always a string.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(Aug-03-2018, 08:06 AM)wavic Wrote: Welcome to the forum! If you print the 'first' variable you will see that it is a string. However, the while condition is against an integer. So... The input is always a string.

Alright, however i have already given the "rock" string (when it is being input) an integer value 5, so why doesn't it still work though?
and when i do add a int() to both inputs i get invalid literal for int().. haha
thanks in advance!
Reply
#4
>>> first = int(input())
5                      # I give an input that can be turned to an integer.
>>> first = int(input())
rock                   # Here the 'rock' word can't be turned to an integer of course so I get an error.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'rock'
If you want to map an integer to a word you can use a dictionary.
first = lower(input("rock paper scissors?: "))   # converting the input to lower case letters so you can used it as a key in the dict bellow
second = lower(input("rock paper scissors?: ")) 

r_p_s = {'rock': 5, 'paper': 10, 'scissors': 15}

while r_p_s['rock']:
    etc.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I have two Same Code but One of them Doesnt Work beginner721 6 3,003 Jan-22-2021, 10:56 PM
Last Post: beginner721
  code doesnt return anything ofrihemo 3 1,987 Jun-30-2020, 05:14 PM
Last Post: ofrihemo
  Why does this work and this doesnt= puruvaish24 1 2,558 May-22-2018, 03:58 AM
Last Post: scidam
  Python code doesnt run tsetsko 3 3,755 Sep-02-2017, 01:47 PM
Last Post: tsetsko

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020