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
  Why doesnt chunk generation work? LotosProgramer 0 250 Jun-18-2021, 01:13 PM
Last Post: LotosProgramer
  if conditions in the for indentation doesnt work ? Sutsro 6 1,317 Jun-15-2021, 11:45 PM
Last Post: bowlofred
  I have two Same Code but One of them Doesnt Work beginner721 6 782 Jan-22-2021, 10:56 PM
Last Post: beginner721
  code doesnt return anything ofrihemo 3 814 Jun-30-2020, 05:14 PM
Last Post: ofrihemo
  taking input doesnt print as list bntayfur 2 804 Jun-04-2020, 02:48 AM
Last Post: bntayfur
  Pydoc documentation doesnt work Cosmosso 4 1,646 Dec-13-2019, 08:21 AM
Last Post: Cosmosso
  multiprocessing doesnt contain some modules(shared_memory) mike000 3 866 Oct-31-2019, 02:20 PM
Last Post: mike000
  BEGINNER: My calculator doesnt work iskov 5 1,245 Oct-09-2019, 07:45 AM
Last Post: buran
  Why this simple function doesnt work? blackknite 8 1,719 Jan-05-2019, 12:32 PM
Last Post: buran
  Why does this work and this doesnt= puruvaish24 1 1,404 May-22-2018, 03:58 AM
Last Post: scidam

Forum Jump:

User Panel Messages

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